开发者

NHibernate search specific subclass

开发者 https://www.devze.com 2022-12-11 17:36 出处:网络
Is it possible to filter on a particular joined-subclass in NHibernate? For example, I have the following classes:

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>();
0

精彩评论

暂无评论...
验证码 换一张
取 消