-->
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.  [ 4 posts ] 
Author Message
 Post subject: Avoid duplicate objects
PostPosted: Fri Apr 15, 2005 6:58 am 
Beginner
Beginner

Joined: Thu Apr 14, 2005 4:29 am
Posts: 28
Hi,

I have a query like this (get all ships which are from a given firm):
List ships = session.createCriteria(Ship.class) .createAlias("relations", "r").add(Restrictions.eq("r.ship", firm)).list();

relations is a inverse relation in ship.hbm.xml:
<bag name="relations" inverse="true" lazy="true" cascade="all">
<key column="ship_id"/>
<one-to-many class="FirmShip"/>
</bag>

The problem is now I get the correct ships but some are double (because they can be double in the relation table firmship)

uniqueResult is no solution because it can be more than one ships. I need a list.

Regards, Detlev


Top
 Profile  
 
 Post subject: Re: Avoid duplicate objects
PostPosted: Fri Apr 15, 2005 7:05 am 
Beginner
Beginner

Joined: Thu Apr 14, 2005 4:29 am
Posts: 28
detlev wrote:
Hi,
List ships = session.createCriteria(Ship.class) .createAlias("relations", "r").add(Restrictions.eq("r.ship", firm)).list();

I made a mistake in my above message:
List ships = session.createCriteria(Ship.class).createAlias("relations", "r").add(Restrictions.eq("r.firm", firm)).list();

r relates to table firmtrade or better class FirmTrade with attributes firm, ship, ownerfrom (date), owneruntil (date)
And have a firm a ship sold and then the same again bought, I have with my query this ship double.

Is it possible with Hibernate to return unique sets?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 16, 2005 2:33 am 
Beginner
Beginner

Joined: Thu Apr 14, 2005 4:29 am
Posts: 28
nobody an idea?

Thank you very much, Detlev


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 16, 2005 6:57 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Code:
Set results = new HashSet( session.createCriteria(Ship.class).createAlias("relations", "r").add(Restrictions.eq("r.firm", firm)).list() );

_________________
Emmanuel


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