Reputation: 1951
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));
How to create criterion if there are odd number of AND parameters eg. 5?
Upvotes: 1
Views: 1906
Reputation: 14751
Just take the result of a previous call to Restrictions.and()
as one of the parameters to Restrictions.and()
For example:
Criterion cr = Restrictions.and(Restrictions.and(Restrictions.eq("A", "X"),
Restrictions.in("B", Arrays.asList("X","Y"))), Restrictions.eq("C", "Z));
Upvotes: 2
Reputation: 11579
You can use DetachedCriteria and it will look like:
Disjunction disjunction = Restrictions.disjunction();
disjunction.add(rest1);
disjunction.add(rest2);
disjunction.add(rest3);
...
dc.add(disjunction);
For AND you can use Conjunction
instead of Disjunction
.
Upvotes: 4