-->
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: Criteria Many-to-Many
PostPosted: Thu Jan 05, 2006 9:09 am 
Newbie

Joined: Fri Nov 11, 2005 4:42 am
Posts: 17
Hibernate version: 3

This is probably an easy question - but...

I have a many-to-many relationship between Arrests & Offenses. I want to use criteria to return all Arrests which contain one or more Offenses from a list. Did that sound right? hmm - how about this. Filter a list of Arrests by whether or not they contain given Offenses? I would like to use the Criteria API as I am already using crits to filter given other criteria (and well I like this API).

Shane
ebersoless@yahoo.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 05, 2006 1:03 pm 
Newbie

Joined: Thu Jan 05, 2006 12:10 pm
Posts: 3
The basic way to do this with criteria would be as follows...



Integer[] ids = {ids to of offense objects to search on};

Criteria criteria = session.createCriteria(Arrest.class);
Criteria offenseCriteria = criteria.createCriteria("offenses");

offenseCriteria.add(Expression.in("id", ids);

Collection<Arrest> arrests = criteria.list();



So in translation... Return a collection of Arrest objects that that manage one or more of the offenses (related by id).

Hope that helps.


Top
 Profile  
 
 Post subject: thanks
PostPosted: Fri Jan 06, 2006 10:14 am 
Newbie

Joined: Fri Nov 11, 2005 4:42 am
Posts: 17
BINGO!!
thanks for the help.

Shane


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 06, 2006 12:50 pm 
Newbie

Joined: Thu Jan 05, 2006 12:10 pm
Posts: 3
One other thing I should point out...

When you set the criteria on the child element you may find that you get multiple instances of the root element (basically you'll get as many parent instances as there are children). The solution to this in criteria query is to apply a result transformer to the query:

criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);


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.