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.  [ 1 post ] 
Author Message
 Post subject: duplicate results using criteria
PostPosted: Tue Apr 20, 2010 10:12 am 
Newbie

Joined: Thu May 01, 2008 8:38 pm
Posts: 2
I know this has been discussed many times. I have Googled, I have searched the forums, but so far no one seems to have a satisfactory solution to this problem.

To summarize the issue, if I have:
Code:
public class A {
   @OneToMany(fetchType=FetchType.EAGER)
   private Set<B> bs;
}

public class B {
   private String prop;
}


Let's say I have one instance of A, and that instance of A has 4 B instances in it's collection, then if I do:

Code:

Criteria crit = session.createCriteria(A.class);
crit.createAlias("bs", "b", Criteria.LEFT_JOIN).add(Restrictions.eq("b.prop", ...);

List results = crit.list();



where the Restriction will match all four B instances, 'results' will have 4 objects in it, when it should have one.

The most often proposed solutions is, pass the List to a Set, where if you've implemented hashCode/equals properly, then, the Set will filter out dups.

The other solution is to call:

Code:

crit.setResultsTransformer(Criteria.DISTINCT_ROOT_ENTITY);



The problem with both of the above solutions is that neither work correctly with paging and I have yet to see an acceptable solution.

What IS the solution here? Is there a good one?

Thanks,
Justin


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

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.