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's what their mapping looks 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?