Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 637675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:38:59+00:00 2026-05-13T20:38:59+00:00

This is the setup for my 2 entities: public class Person { public Guid

  • 0

This is the setup for my 2 entities:

public class Person {
  public Guid Id {get;set;}
  public string Name {get;set;}
}

public class Immortal : Person {
  public string DarkName {get;set;}
}

Here is what their mappings look like:

<class name="Person">
  <id name="Id">
    <generator class="guid.comb"/>
  </id>
  <property name="Name" />

  <joined-subclass name="Immortal">
    <key column='PersonId' />
    <property name="DarkName" />
  </joined-subclass>
</class>

So that’s the setup, 2 entities, one is a joined subclass of the other. I have a search framework that takes any number of criteria from a form and then applies the appropriate criteria to a query, then returns the results.

Now say I have a single form field in this case, “Name” — I want to bring back all people, be they normal Persons or that special class of beings Immortals, by seeing if their name matches the Name property of Person, but in the case of an Immortal, I’d also like to count it as a match if their DarkName matches what was given in the form.

So this is my dilemma, how do I do this? The best I have been able to do with NHibernate’s ICriteria stuff is to make a subquery on Immortal to check the name there, and then see if the root Person id is in that subquery. When working with a table of 10s of thousands of Persons, however, this method of getting results is extraordinarily inefficient and can even time out the request (30s+) in my real world situation.

I would be open to doing this in HQL too, because I figure I want an outer join on Immortal to check for this field, but I cannot get HQL to do a join on 2 disparate entities on an arbitrary property — joins as far as I know must be based on direct associations in your mappings. For instance, this is what I would like to see:

select person from Person person
  outer join Immortal immortal on immortal.PersonId = person.Id
  where
    person.Name = :name or
    immortal.DarkName = :name

What say you stackoverflow?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:38:59+00:00Added an answer on May 13, 2026 at 8:38 pm

    The following criteria query produces almost exactly the SQL that you want.

    It takes advantage of some laxness in the criteria query engine.

    var list = session.CreateCriteria<Person>()
        .Add( Expression.Disjunction()
            .Add( Expression.Eq( "Name", name ) )
            .Add( Expression.Eq( "ImmortalName", name ) )
            )
        .List<Person>();
    

    produces the following SQL:

    SELECT this_.Id             as Id0_0_,
           this_.Name           as Name0_0_,
           this_1_.ImmortalName as Immortal2_1_0_,
           case 
             when this_1_.Id is not null then 1
             when this_.Id is not null then 0
           end as clazz_0_
    FROM   person this_
           left outer join immortal this_1_
             on this_.Id = this_1_.Id
    WHERE  (this_.Name = 'foo' /* @p0 */
             or this_1_.ImmortalName = 'foo' /* @p1 */)
    

    I used the following class mapping:

    <class name="Person" table="person">
        <id name="Id">
            <generator class="identity" />
        </id>
        <property name="Name" />
        <joined-subclass name="Immortal" table="immortal">
            <key column="Id" />
            <property name="ImmortalName" />
        </joined-subclass>
    </class>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get an Apache server running on my Cygwin setup to follow
I have a large table that I want to map to a few entities.
I have two entities: Project, Employee Employee has primary key {employeeId} + some other
Been messing around with Hibernate and PostgreSQL trying to get it to work as
I've just ran into an unexpected behaviour for Entity framework entities in use with
I keep getting this error when trying to delete a record from my table.
I have a setup on a EC2 instance that uses Whirr to spin up
Currently I'am working on an MVC project in which I try to get a
I was tinkering with this a few days ago and it seemed to be
I am trying to use an early experimental release of mapper implementation to empty

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.