How do you "OR" criteria together when using a criteria query with hibernate?

JavaHibernate

Java Problem Overview


I'm trying to do a basic "OR" on three fields using a hibernate criteria query.

Example

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

I'd like to build a criteria query where my search string could match "name" or "address" or "phoneNumber".

Java Solutions


Solution 1 - Java

You want to use Restrictions.disjuntion(). Like so

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 doc here.

Solution 2 - Java

Assuming you have a hibernate session to hand then something like the following should work:

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);

Solution 3 - Java

    //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.

Solution 4 - Java

//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.

Solution 5 - Java

Just in case anyone 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)));

Solution 6 - Java

The conditions can be applied using the or / and in different levels of the query using disjunction

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();

Solution 7 - Java

This is what worked for me for an OR condition, that too with an IN condition and not the answer up-voted most on 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"})
				    ));

Resulting Query:

  and (
            this_.XXXX=? 
            or this_.YYYY in (
                ?, ?, ?
            )
        ) 

Solution 8 - Java

If someone is using CriteriaQuery instead of Criteria, you can put all your expressions in a Predicate list and put a OR by predicates size like this:

List<Predicate> predicates = new ArrayList<>();
if (...) {
  predicates.add(...);
}

criteriaQuery.where(cb.or(predicates.toArray(new Predicate[predicates.size()])));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionScArcher2View Question on Stackoverflow
Solution 1 - JavasblundyView Answer on Stackoverflow
Solution 2 - JavaRob OxspringView Answer on Stackoverflow
Solution 3 - JavaDharmender RawatView Answer on Stackoverflow
Solution 4 - JavaDharmender RawatView Answer on Stackoverflow
Solution 5 - JavaGeir-Tore LindsveView Answer on Stackoverflow
Solution 6 - JavaTiago MediciView Answer on Stackoverflow
Solution 7 - JavaronakView Answer on Stackoverflow
Solution 8 - JavaGasparView Answer on Stackoverflow