title: Difference in where clause beetwen JPQL and CriteriaBuilder
body: <p>I have two separate entity class: Job and JobOrgUnitCfg
  Below I put two pieces of code make this same select on that objects.</p>
  
  <p>First query in JPQL:</p>
  
  <pre><code>    Query jobOrgUnitCfgQuery = entityManager.createQuery(
              "SELECT c FROM JobOrgUnitCfg c WHERE c.orgId = :orgId and c.schedulerNextActivation &lt; current_timestamp and c.active = :active and " +
              " not exists (SELECT j  FROM Job j WHERE j.orderId = c.orderId and j.orgId = c.orgId and j.status &lt;&gt; :jobStatus)");
  
      jobOrgUnitCfgQuery.setParameter("orgId", orgId);
      jobOrgUnitCfgQuery.setParameter("jobStatus", JobStatusEnum.End);
      jobOrgUnitCfgQuery.setParameter("active", Boolean.TRUE);
      return jobOrgUnitCfgQuery.getResultList();
  </code></pre>
  
  <p>and second query build with CriteriaBuilder:</p>
  
  <pre><code>    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
      CriteriaQuery&lt;JobOrgUnitCfg&gt; criteria = cb.createQuery(JobOrgUnitCfg.class);
      Root&lt;JobOrgUnitCfg&gt; jobOrgUnitCfgRoot = criteria.from(JobOrgUnitCfg.class);
  
      Subquery&lt;Job&gt; subquery = criteria.subquery(Job.class);
      Root&lt;Job&gt; jobRoot = subquery.from(Job.class);
  
      subquery.where(
              cb.and(
                      cb.equal(jobRoot.get(Job_.orderId), jobOrgUnitCfgRoot.get(JobOrgUnitCfg_.orderId)),
                      cb.equal(jobRoot.get(Job_.orgId), jobOrgUnitCfgRoot.get(JobOrgUnitCfg_.orgId)),
                      cb.not(cb.equal(jobRoot.get(Job_.status), JobStatusEnum.End))
              )
      );
  
      Predicate where = cb.and(cb.equal(jobOrgUnitCfgRoot.get(JobOrgUnitCfg_.orgId), orgId),
              cb.lessThan(jobOrgUnitCfgRoot.get(JobOrgUnitCfg_.schedulerNextActivation), cb.currentTimestamp()),
              cb.equal(jobOrgUnitCfgRoot.get(JobOrgUnitCfg_.active), Boolean.TRUE),
              cb.not(cb.exists(subquery))
      );
  
      criteria.where(where);
      TypedQuery&lt;JobOrgUnitCfg&gt; query = entityManager.createQuery(criteria);
      return query.getResultList();
  </code></pre>
  
  <p>First generate wrong where clause (duplicate table S_JOBS_ORG_UNIT_CFG inside subselect):</p>
  
  <pre><code>SELECT ... FROM S_JOBS_ORG_UNIT_CFG t0 
  WHERE ((((t0.ORG_ID = ?) AND (t0.SCHEDULER_NEXT_ACTIVATION &lt; SYSDATE)) AND (t0.ACTIVE = ?)) 
  AND NOT EXISTS (SELECT ? FROM S_JOBS_ORG_UNIT_CFG t2, S_JOBS t1 WHERE ((((t1.ORDER_ID = t2.ORDER_ID) AND (t1.ORG_ID = t2.ORG_ID)) AND (t1.STATUS &lt;&gt; ?)) AND (t0.ORDER_ID = t2.ORDER_ID))) )
  </code></pre>
  
  <p>sql from Criteriabuilder is right:</p>
  
  <pre><code>SELECT ... FROM S_JOBS_ORG_UNIT_CFG t0 
  WHERE ((((t0.ORG_ID = ?) AND (t0.SCHEDULER_NEXT_ACTIVATION &lt; SYSDATE)) AND (t0.ACTIVE = ?)) 
  AND NOT EXISTS (SELECT ? FROM S_JOBS t1 WHERE (((t1.ORDER_ID = t0.ORDER_ID) AND (t1.ORG_ID = t0.ORG_ID)) AND NOT ((t1.STATUS = ?)))) )
  </code></pre>
  
  <p>Can someone explain me what is wrong in my JPQL ?</p>