Array iteration to create a sleep mode statement

let my array have 3 values โ€‹โ€‹of an integer object = 3,4,5 I will need to create sleep criteria that look below

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)))))); 

the above criteria are created manually, I'm wondering if this can be automated using iteration

 for(Iterator iterator = integerArray.iterator; iterator.hasNext()){ // create the criteria above } 
+4
source share
2 answers

Yes, you can use Disjunction in your loop:

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

You can use the in constraint while accepting an Array argument.

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

All Articles