Filter alias/relation based on concrete class

1 view
Skip to first unread message

Matt

unread,
Sep 16, 2008, 11:12:44 AM9/16/08
to nhusers
Using NHibernate 2.0, how can I return all root Person entities that
have a dog, say for example I want to return all People that have a
Pet of type Dog, where Pets in a polymorphic IPet collection on a
Person. I have tried the NHibernate documentation but I can't find
how to handle this. I'm using a table per subclass (with joined-
subclass mapping to handle the Pet hierachy) so no discriminator value
explicitly exists.

Thanks

Will Shaver

unread,
Sep 16, 2008, 2:37:00 PM9/16/08
to nhu...@googlegroups.com
I don't know how this is supposed to be solved (and would love to know
myself) but ended up mapping the class type column as a property in my
class thereby allowing me to access it via HQL/Criteria.

Ken Egozi

unread,
Sep 16, 2008, 3:20:21 PM9/16/08
to nhu...@googlegroups.com
if Pet has relation to Person then this should work:

from Person person
where exists (from Dog dog where dog.Person = person)

I *think*

Wolfgang Trog

unread,
Sep 17, 2008, 2:50:03 AM9/17/08
to nhu...@googlegroups.com
According to the doc:

"Likewise, the special property class accesses the discriminator value of an
instance in the case of polymorphic
persistence. A Java class name embedded in the where clause will be
translated to its discriminator value.

from Eg.Cat cat where cat.class = Eg.DomesticCat"

so I would try:

from Person p join p.Pets pet where pet.class = Dog

rashack

unread,
Sep 17, 2008, 7:16:22 AM9/17/08
to nhusers
Can you achieve the same behaviour with Criteria? I have tried but no
luck...

Cheers,
Jarda

Wolfgang Trog

unread,
Sep 18, 2008, 1:59:09 AM9/18/08
to nhu...@googlegroups.com
There's another thread covering that problem. And there Fabio posted the
solution for criterias:

http://groups.google.com/group/nhusers/msg/a688fb5ca7d55950

from Person p join p.Pets pet where pet.class = Dog

would be

s.CreateCriteria(typeof(Person), "p")
.CreateCriteria("p.Pets", "pet")
.Add(Property.ForName("pet.class").Eq(typeof(Dog)
.List();

--
Wolfgang


Reply all
Reply to author
Forward
0 new messages