-->
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.  [ 2 posts ] 
Author Message
 Post subject: "Reverse" a join with the Criteria API
PostPosted: Tue Mar 28, 2006 4:54 am 
Newbie

Joined: Wed Nov 02, 2005 6:07 am
Posts: 12
Hibernate version: 3.xx

Hi,

I've got table A and table B. In most cases I process a join between these tables to get the information I need.

Code:
Criteria crit = sess.createCriteria(CustomerAddress.class)
            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)

           crit.createAlias("B", "B");

......

With this I get every entry of table A which has an entry in table B. But now I've got an understanding problem. How is it possible to get every entry in table A which has NO entry in table B?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 12:48 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Assuming that B is a set, map or similar, then you want something like this:
Code:
Criteria crit = sess.createCriteria(A.class);
crit.setProjection(Projections.alias(Projections.count("B"), "count"));
crit.add(Restrictions.eq("count", 0));
You could also use subqueries, but this is the most efficient.


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