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开发者_如何学Python { 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)...
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>();
精彩评论