Hibernate Criteria Restrictions AND / OR combination

girishsovflow picture girishsovflow · Nov 14, 2011 · Viewed 130.4k times · Source

How would I achieve this using Hibernate Restrictions?

(((A='X') and (B in('X',Y))) or ((A='Y') and (B='Z')))

Answer

osdamv picture osdamv · Nov 14, 2011

think works

Criteria criteria = getSession().createCriteria(clazz); 
Criterion rest1= Restrictions.and(Restrictions.eq(A, "X"), 
           Restrictions.in("B", Arrays.asList("X",Y)));
Criterion rest2= Restrictions.and(Restrictions.eq(A, "Y"), 
           Restrictions.eq(B, "Z"));
criteria.add(Restrictions.or(rest1, rest2));