views:

188

answers:

1

Is it possible to filter on a particular joined-subclass in NHibernate?

For example, I have the following classes:

Pet { Name }
Cat: Pet { Paws }
Budgie: Pet { Wings }
Person { Pets }

I want to create an NHibernate search to give me Persons with Cats with 4 paws.

I can only seem to be able to restrict on a Pet's attributes (Name)...

A: 

You should try something like this. Haven't tested it though, so I'm not 100% sure.

 DetachedCriteria fetchCatsWith4Pawns = DetachedCriteria.For<Cat>();
 fetchCatsWith4Pawns.Add(Restrictions.Eq("Pawns", 4));
 fetchCatsWith4Pawns.SetProjection(Projections.Id());

 DetachedCriteria fetchPersonsWithCatsWith4Pawns = DetachedCriteria.For<Person>();
 fetchPersonsWithCatsWith4Pawns.CreateCriteria("Pets", "pet").Add(Subqueries.PropertyIn("pet.id", fetchCatsWith4Pawns));
 fetchPersonsWithCatsWith4Pawns.GetExecutableCriteria(session).List<Person>();
BennyM
Thanks! You are correct :)
Mark