• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate either / or query?

 
Erik Blank
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using hibernate, how do I make an "OR" query between two criteria?


For example, we start with this root Criteria:

Then we instantiate two associated criteria...

Now how do I specify that my pCrit query should return matches from either/or of these two criteria?
It is important that I can keep / access the criteria objects (e.g. clubCrit, hobbyCrit).

Thanks!
 
wangch ch
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can either "or" one more restriction to the result:

Criteria criteria = session.createCriteria(Reader.class);
criteria.add(Restrictions.or(Restrictions.or(condition1, condition2), condition3));

or use Disjunction:

Criteria criteria = session.createCriteria(Reader.class);
Junction conditionGroup = Restrictions.disjunction();
conditionGroup.add(condition1).add(condition2).add(condition3);
criteria.add(conditionGroup);


from http://stackoverflow.com/questions/5859058/how-to-make-a-criteria-query-with-3-or-criterions-properly
 
Alex Armenteros
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you post an aproximate representation of your model, we could help you better.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic