How do you compare the criteria "OR" when using the query criteria with hibernation?

I am trying to make a basic "OR" in three fields using the query hibernation criteria.

Example

class Whatever{ string name; string address; string phoneNumber; } 

I would like to create a query of criteria where my search string can match "name" or "address" or "phone number".

+75
java hibernate
Sep 11 '08 at 20:04
source share
7 answers

You want to use Restrictions.disjuntion() . In this way

 session.createCriteria(Whatever.class) .add(Restrictions.disjunction() .add(Restrictions.eq("name", queryString)) .add(Restrictions.eq("address", queryString)) .add(Restrictions.eq("phoneNumber", queryString)) ); 

See the Hibernate document here .

+126
Sep 11 '08 at 20:23
source share

Assuming you have a hibernation session, do the following:

 Criteria c = session.createCriteria(Whatever.class); Disjunction or = Restrictions.disjunction(); or.add(Restrictions.eq("name",searchString)); or.add(Restrictions.eq("address",searchString)); or.add(Restrictions.eq("phoneNumber",searchString)); c.add(or); 
+65
Sep 11 '08 at 20:31
source share
  //Expression : (c1 AND c2) OR (c3) Criteria criteria = session.createCriteria(Employee.class); Criterion c1 = Restrictions.like("name", "%e%"); Criterion c2 = Restrictions.ge("salary", 10000.00); Criterion c3 = Restrictions.like("name", "%YYY%"); Criterion c4 = Restrictions.or(Restrictions.and(c1, c2), c3); criteria.add(c4); 

// The same can be done for (c1 OR c2) AND c3 or any complex expression.

+10
Aug 13 '14 at 10:37
source share
 //Expression : (c1 AND c2) OR (c3) Criteria criteria = session.createCriteria(Employee.class); Criterion c1 = Restrictions.like("name", "%e%"); Criterion c2 = Restrictions.ge("salary", 10000.00); Criterion c3 = Restrictions.like("name", "%YYY%"); Criterion c4 = Restrictions.or(Restrictions.and(c1, c2), c3); criteria.add(c4); //Same thing can be done for (c1 OR c2) AND c3, or any complex expression. 
+6
Aug 13 '14 at 19:09
source share

Just in case, someone should stumble upon this with the same question for NHibernate:

 ICriteria c = session.CreateCriteria(typeof (Whatever)) .Add(Expression.Disjunction() .Add(Expression.Eq("name", searchString)) .Add(Expression.Eq("address", searchString)) .Add(Expression.Eq("phoneNumber", searchString))); 
+3
Sep 12 '08 at 10:40
source share

Conditions can be applied using and / or at different levels of the query using a clause

 Criteria query = getCriteria("ENTITY_NAME"); query.add(Restrictions.ne("column Name", current _value)); Disjunction disjunction = Restrictions.disjunction(); if (param_1 != null) disjunction.add(Restrictions.or(Restrictions.eq("column Name", param1))); if (param_2 != null) disjunction.add(Restrictions.or(Restrictions.eq("column Name", param_2))); if (param_3 != null) disjunction.add(Restrictions.or(Restrictions.eq("column Name", param_3))); if (param_4 != null && param_5 != null) disjunction.add(Restrictions.or(Restrictions.and(Restrictions.eq("column Name", param_4 ), Restrictions.eq("column Name", param_5 )))); if (disjunction.conditions() != null && disjunction.conditions().iterator().hasNext()) query.add(Restrictions.and(disjunction)); return query.list(); 
+1
Apr 08 '16 at 7:45
source share

This is what worked for me for the OR condition, which is also with the IN condition, and not with the answer that received the most votes in this discussion:

 criteria.add( Restrictions.or( Restrictions.eq(ch.getPath(ch.propertyResolver().getXXXX()), "OR_STRING"), Restrictions.in(ch.getPath(ch.propertyResolver().getYYYY()), new String[]{"AA","BB","CC"}) )); 

Result Query:

  and ( this_.XXXX=? or this_.YYYY in ( ?, ?, ? ) ) 
0
Jun 21 '19 at 6:46
source share



All Articles