Suppose you have a base class Person and two derived classes Customer and Employee. All objects are from the same table and inheritance is mapped through discriminators.
Now lets create a new class, say Document:
class Document
{
IList EmployeeList;
IList CustomerList;
}
(bare with me as this design is a MUST for me and my real situation is much more complicated and has to be programmed this way and I prefer not to map it in a single collection. My hierarchy has aprox 15 classes on the same table because the use case requires so).
If I do something like:
Session.CreateCriteria(typeof(Document))
.SetFetchMode("EmployeeList", FetchMode.Join)
.List();
At this point NH generates somthing like
"select ... from document inner join person on d.fk = p.id"
When the loader parses the results it will get at some point a row with the discriminator value corespondent to Customer (instead of Employee) and so it throw an WrongClassException as the discriminator in the row does not correspond to the given hierarchy.
The reason it throws this exception is that it does not generate a "where discriminator_col in (....)" in the select clause as it does when loading the collection lazy.
I also tried a workaround and mapped both lists like:
<bag name=...... where="discriminator_col = XXX">
..
</bag>
But, surprise, when fetched using join in criteria the generated sql does not contain the where expression that I set on the bag. When the bag is loaded separately the where condition is generated.
I think I should get this into JIRA but maybe someone knows what I've done wrong or knows a workaround.
Thanks,
Dragos
PS: if needed I can recreate the situation for a test case...
|