title: JPA Criteria API: Iterate through list and create predicates
body: <p><br>
  I'm trying to migrate an iBatis project into rest service using Spring boot, Spring data JPA and Hibernate. I'm stuck at a query conversion, any help is appreciated. This is the iBatis query I'm trying to migrate.</p>
  
  <pre><code>&lt;foreach item="item" collection="currentStatus"&gt;
      &lt;choose&gt;
          &lt;when test="item in fStatusList"&gt;
              ((j_status_code != #{item}) AND (f_status_code = #{item}) AND
              (EXP_FLAG IS NULL OR EXP_FLAG ='F' ))
          &lt;/when&gt;
          &lt;when test="item in jStatusList"&gt;
              ((max_jsc.status_code = #{item}) AND
                  &lt;if test="item ==419"&gt;
                      EXP_FLAG='H'
                  &lt;/if&gt;
                  &lt;if test="item ==449"&gt;
                      EXP_FLAG='C'
                  &lt;/if&gt;
              &lt;/when&gt;
          &lt;otherwise&gt;
              ((j_status_code = #{item}) AND (f_status_code_name is null))
          &lt;/otherwise&gt;
      &lt;/choose&gt;
  &lt;/foreach&gt;
  </code></pre>
  
  <p>This is what I have so far.</p>
  
  <pre><code>    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
      CriteriaQuery&lt;Job&gt; cq = cb.createQuery(Job.class);
      Root&lt;Job&gt; job = cq.from(Job.class);
      List&lt;Predicate&gt; predicates = new ArrayList&lt;&gt;();
      for (int status : statusList) {
      if (IntStream.of(fStatusList()).anyMatch(x -&gt; x == status)) {
          predicates.add(cb.equal(job.get("f_status_code"), status));
          predicates.add(
              cb.or(
                  cb.isNull(job.get("EXP_FLAG")),
                  cb.equal(
                      job.get("EXP_FLAG"),"F")));
        } else if (IntStream.of(jStatusList()).anyMatch(x -&gt; x == status)) {
          predicates.add(cb.equal(job.get("jobStatusCode"), status));
          if (status == 10) {
            predicates.add(
                cb.equal(
                    job.get("EXP_FLAG"), "H"));
          } else if (status == 15) {
            predicates.add(
                cb.equal(
                    job.get("EXP_FLAG"), "C"));
          }
        } else {
          predicates.add(
              cb.and(
                  cb.isNull(job.get("f_status_code")),
                  cb.equal(job.get("j_status_code"), status)));
        }
  }
      cq.where(predicates.toArray(new Predicate[0]));
      TypedQuery&lt;Job&gt; query = entityManager.createQuery(cq);
  </code></pre>
  
  <p>The problem is that the where condition is getting formed like below. The iteration needs to add OR condition, instead of AND for every statuses. How can I achieve this ?<br> </p>
  
  <pre><code>where
   (
   job0_.f_status_code is null
   ) 
   and job0_.j_status_code=315 
   and job0_.j_status_code=449 
   and job0_.exp_flag=?
  </code></pre>
  
  <p>Also please advise if  using JPA Criteria API is the best option in these scenarios. It seemed pretty complicated and unreadable  for some simple iBatis conditions. Is there a better alternative while using Hibernate ?</p>