-->
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: Querying for an empty set in a LEFT JOIN
PostPosted: Fri Nov 04, 2005 7:57 pm 
Newbie

Joined: Thu Sep 08, 2005 12:07 pm
Posts: 6
Using Hibernate 3.0 on a Postgresql 8.0 db, I need to query for a list of objects that have an empty set in a nested association.

Tables:
tray
rack
sample

Associations:
tray contains 1 or more racks
rack contains 0 or more samples

I need a list of trays that have no samples in any of its racks. I have been partially successful using the following Hibernate criteria query...

emptyTrays = HibernateUtil.getSession().createCriteria(Tray.class)
.createCriteria("racks", "rack")
.add(Expression.isEmpty("rack.samples"))
.list();

The only problem seems to be that there are several copies of the same tray in the list... one for each rack in the tray. For example, if there is 1 empty tray, and that tray has 6 racks, then the resulting list will have 6 Tray objects in it instead of one.

I could do post-processing on the list, but I would like to know if there is a way to use Hibernate to generate a unique list of Tray objects that contain no samples.

Thanks
Eric-

_________________
Eric Hansen


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 05, 2005 8:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Use the Criteria.setResultTransformer(), using the DistinctRootEntityTransformer class.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 05, 2005 9:23 am 
Newbie

Joined: Thu Sep 08, 2005 12:07 pm
Posts: 6
Excellent!!!
emptyTrays = HibernateUtil.getSession().createCriteria(Tray.class)
.createCriteria("racks", "rack")
.add(Expression.isEmpty("rack.samples"))
.setResultTransformer(new DistinctRootEntityResultTransformer())
.list();

now returns exactly what I need.

Perhaps a bit easier to remember would be the equivalent form...
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)


btw,
I didn'tfind any mention of ResultTransformer in 'Hibernate In Action'

_________________
Eric Hansen


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.