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));
如果有奇数个AND参数,如何创建标准,例如。 5?
答案 0 :(得分:4)
您可以使用DetachedCriteria,它看起来像:
Disjunction disjunction = Restrictions.disjunction();
disjunction.add(rest1);
disjunction.add(rest2);
disjunction.add(rest3);
...
dc.add(disjunction);
对于AND,您可以使用Conjunction
代替Disjunction
。
答案 1 :(得分:2)
只需将之前调用Restrictions.and()
的结果作为Restrictions.and()的参数之一
例如:
Criterion cr = Restrictions.and(Restrictions.and(Restrictions.eq("A", "X"),
Restrictions.in("B", Arrays.asList("X","Y"))), Restrictions.eq("C", "Z));