-->
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.  [ 3 posts ] 
Author Message
 Post subject: Duplicate results with "join fetch" for array resu
PostPosted: Thu Jun 08, 2006 4:07 pm 
Newbie

Joined: Thu Jun 08, 2006 10:51 am
Posts: 1
Queries with a join fetch can return duplicate results
for example :

I have classes A and B
A has a collection of B

The following query would return a A having 3 B in his collection
"from A a join fetch B where a.id = 1"

The resulting list contains 3 elements, all the same instance of A. Duplicates can be eliminated by putting the list into a HashSet, as suggested in this article :
http://www.hibernate.org/117.html#A11
And this works fine, because HashSets don't allow doplicates, and only 1 will be stored in the HashSet.



The real problem is when the query is on more than one entity.
For example :

I have classes A, B, X
A has a collection of B

The following query would return a A having 3 B in his collection, and only 1 X associated to this A :
"from A a, X x join fetch B where a.something = x.something and a.id = 1"

So the resulting list contains 3 elements of the type Object[2], each of these elements containing a A and a X. It is the same instance of A and X in all these 3 elements.

The suggested solution from the article doesnt work here. Putting it in a HashSet won't eliminate duplicates. A HashSet doesn't allow duplicates to be added, but it detect a duplicate by calling .equals() on the objects. But for a array like our Object[2], calling .equals() doesn't really compare the contents, the real way to compare arrays is with Arrays.equals(array1, array2).

Did anyone face this problem before? And if so, how did you solve it?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 08, 2006 6:25 pm 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
one of the way would be using criteria
with

criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 09, 2006 12:53 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The equivalent in HQL is to use select distinct:
Code:
select distinct a, x
from A a, X x join fetch x.B
where a.something = x.something and a.id = 1

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.