These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Criteria API Many-To-Many Query
PostPosted: Mon Apr 06, 2009 2:23 pm 
Newbie

Joined: Mon Apr 06, 2009 2:21 pm
Posts: 1
Hello,

I've the following many to many relation: File 1 --- * File_Insurer * --- 1 Insurer. I'm trying to query this relation using the Criteria API to get Files that meet ALL specified Insurers (Get all Files where Insurer.Id == 2 AND Insurer.Id == 3).

I tried many solutions:

DetachedCriteria dc = DetachedCriteria.For<File>();
dc.SetResultTransformer(new DistinctRootEntityResultTransformer());
dc.CreateCriteria("Insurers").Add(Expression.Eq("Id", long.Parse("2")));
dc.CreateCriteria("Insurers").Add(Expression.Eq("Id", long.Parse("3")));
List<File> searchResults = File.FindAll(dc).ToList<File>();

That gives me a error "duplicate association path: Insurers".

Next option:

DetachedCriteria dc = DetachedCriteria.For<File>();
dc.SetResultTransformer(new DistinctRootEntityResultTransformer());
dc.CreateCriteria("Insurers").Add(Expression.And(Expression.Eq("Id", long.Parse("3")), Expression.Eq("Id", long.Parse("2"))));
List<File> searchResults = File.FindAll(dc).ToList<File>();

The result list is empty (but shouldn't).

Next option with alias:

DetachedCriteria dc = DetachedCriteria.For<File>();
dc.SetResultTransformer(new DistinctRootEntityResultTransformer());
dc.CreateAlias("Insurers", "i").Add(Expression.Eq("i.Id", long.Parse("2"))).Add(Expression.Eq("i.Id", long.Parse("3")));
List<File> searchResults = File.FindAll(dc).ToList<File>();

The result list is empty again - strange.

Next try:

DetachedCriteria dc = DetachedCriteria.For<File>();
dc.SetResultTransformer(new DistinctRootEntityResultTransformer());
List<long> insurerIds = new List<long>();
insurerIds.Add(2);
insurerIds.Add(3);
dc.CreateCriteria("Insurers").Add(Expression.In("Id", insurerIds));
List<File> searchResults = File.FindAll(dc).ToList<File>();

This works somehow, but the result set contains a all possible options (OR) - it's not an exact match.

I'm very confused on this topic. Any help would be highly appreciated.

Regards,
Jakub


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 07, 2009 12:51 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Try this:

Code:
DetachedCriteria dc = DetachedCriteria.For<File>("f")
     .SetResultTransformer(new DistinctRootEntityResultTransformer());
     .Add(Subqueries.Eq(numOfSpecifiedInsureres, inDc));

DetachedCriteria inDc = DetachedCriteria.For<Insurer>("i")
        .Add(PropertyEq("f.Insurer", "i.Id"))
        .Add(Expression.Or(Expression.IdEq(2),Expression.IdEq(3))
        .SetProjection(Projections.CountDistinct("Id"));



I'm not sure if the Property.Eq(...) works correctly, but the rest shoulr work. You problem is that you want all files which are linked to one or more insureres at the same time. The queries you tried, always tested if the insurer id of one link is 2 and 3 which is impossible.
In the above query you have to dynamically set numOfSpecifiedInsureres. In your example 2.

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.