1
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?

4

2 回答 2

4

您可以使用 DetachedCriteria,它看起来像:
Disjunction disjunction = Restrictions.disjunction();
disjunction.add(rest1);
disjunction.add(rest2);
disjunction.add(rest3);
...
dc.add(disjunction);

对于 AND,您可以使用Conjunction而不是Disjunction.

于 2013-01-08T22:38:42.030 回答
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));
于 2013-01-08T22:38:55.010 回答