2010-01-12 20 views
4

讓說我的陣列具有3個整數對象值= 3,4,5 我需要創建看起來像下面迭代陣列來創建休眠條件語句

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 
} 

回答

11

是的,你可以用你的循環Disjunction

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

您可以使用in限制而需要Array參數自動執行此。

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