-->
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.  [ 5 posts ] 
Author Message
 Post subject: joining associations without duplicate elements
PostPosted: Thu Dec 02, 2004 5:28 pm 
Newbie

Joined: Fri Nov 05, 2004 8:48 am
Posts: 3
Location: Germany, Hamburg
Hi,

i am in trouble with a SQL-Statement and Hibernate. I am using two associatiuon the tabel/object order and the table/object item. The item store a property field status.

The SQL statement returns the orders with the status XX orderd by the date:

SELECT o
FROM ShopOrder o
INNER JOIN FETCH o.items item
WHERE item.status = :orderItemStatus
ORDER BY o.date

But this statement returns duplicate Elements (because the cartesian product). In Hibernate in Action is written: "Hibernate doesn't return a distinct result list...... You wil probably need to make the result distinct yourself using, for example distinctResult = new HashMap(resultList).".

OK the trick with the set works fine but i lost the order! I tried several statements and the operator distinct but no succsess. Do I have to Convert the result in a Set (to make it distinct), return to a list for handmake sorting? Or can get an orderd List without duplicate elements with HSQL? Maby you can give me a tip. I have no idea how.

Thanxx
Marc


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 02, 2004 6:12 pm 
Beginner
Beginner

Joined: Wed Nov 19, 2003 6:46 pm
Posts: 41
Location: Auckland, New Zealand
Try using a LinkedHashSet instead.

Here's a snippet of code:

Code:
Set set = new LinkedHashSet();
set.addAll(query.list());   
return set;


Got this from the Hibernate FAQ:

http://www.hibernate.org/117.html

HTH

Craig


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 6:02 am 
Newbie

Joined: Fri Nov 05, 2004 8:48 am
Posts: 3
Location: Germany, Hamburg
It works perfectly! Thanxx Craig


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 5:08 pm 
Regular
Regular

Joined: Mon Jun 13, 2005 12:38 pm
Posts: 56
Location: Austin, TX
Is this really the way it should be done? For some reason this feels like a work around. Could a hibernate developer comment on this?

thanks in advance,
Mike


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 6:41 pm 
Regular
Regular

Joined: Mon Jun 13, 2005 12:38 pm
Posts: 56
Location: Austin, TX
Looks like the "correct" way of doing this is by using a criteria object with:

.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)

Now my question is...how do I do this in HQL.


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