-->
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: Criteria API question - OR-ing over 2 collections
PostPosted: Mon Jul 30, 2007 10:33 am 
Newbie

Joined: Tue Apr 03, 2007 11:40 am
Posts: 7
Location: London
I have a class with a couple of collections..

public class Project
private List<Organisation> organisations;
private List<Person> contacts;

And in a certain case, I'd like to return all Projects for which a supplied Organisation id matches an Organisation in the 'organisations' collection, OR for which a supplied Person ID matches a Person in the 'contacts' collection.

I can run a search on either one of these collections...

Criteria criteria = session.createCriteria(Project.class);
...
criteria.createCriteria("contacts")
.add(Restrictions.like("id", argument.getContactId()));

criteria.createCriteria("organisations")
.add(Restrictions.like("id", argument.getOrganisationId()));

.. but I can't figure out how to do an OR over the two - I'm finding the Criteria API pretty non-intuitive (no doubt it will get easier over time!)

Could anyone recommend an approach for the above ?

thanks,

matt.

_________________
----
Matt


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 30, 2007 1:26 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Rather than createCriteria for the OR Restrictrictions use the following.

Code:
Criteria criteria = session.createCriteria(Project.class);
...
criteria.createAlias("contactsAlias", "contacts");
criteria.createAlias("organizationsAlias", "organisations")

Disjunction disjunction = Restrictions.disjunction();
criteria.add(disjunction.add(Restrictions.like("contactsAlias.id", argument.getContactId()).add(Restrictions.like("organizationsAlias.id", argument.getOrganisationId()));


Bascially the createAlias will create a join (inner join by default) to your associations and then the disjunction will do the OR.

The brackets, etc in this above sample may be way off as I didn't compile it or anything but you should be able to get the gist.

Bear in mind that the default join for the create alias is an inner join so you might have to define an outer join type in your case.

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 08, 2007 6:51 am 
Newbie

Joined: Tue Apr 03, 2007 11:40 am
Posts: 7
Location: London
Thanks, that helps.

I ended up with this..

criteria.createAlias("contacts", "contactsAlias");
criteria.createAlias("organisations", "organisationsAlias");

criteria.add
(Restrictions.or
((Restrictions.eq("contactsAlias.id",argument.getContactId())),
(Restrictions.eq("organisationsAlias.id", argument.getOrganisationId())) ));

_________________
----
Matt


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.