迭代数组以创建hibernate条件语句

时间:2010-01-12 01:51:45

标签: java hibernate

假设我的数组有3个整数对象值= 3,4,5 我需要创建如下所示的hibernate标准

criteria.add(Restrictions.and(Restrictions.not(Restrictions.eq(
        "stepId", new Integer(3))), Restrictions.and(Restrictions
        .not(Restrictions.eq("stepId", new Integer(4))), Restrictions
        .not(Restrictions.eq("stepId", new Integer(5))))));

上述标准是手动创建的,我想知道可以通过迭代自动化

for(Iterator iterator = integerArray.iterator; iterator.hasNext()){
    // create the criteria above
}

2 个答案:

答案 0 :(得分:11)

是的,您可以在循环中使用Disjunction

Disjunction disjunction = Restrictions.disjunction();
for(Iterator iterator = integerArray.iterator; iterator.hasNext()){
    disjunction.add(yourRestriction); //add your restirction here
}
criteria.add(disjunction );

答案 1 :(得分:7)

您可以在Array参数时使用in限制。

  Integer[] integerArray = ...
  criteria.add(Restrictions.and(Restrictions.not(
        Restrictions.in("stepId", integerArray)
  );