title: JPA Criteria Disjunction
body: <p>Im trying to learn and understand JPA criteria.
  So far i am very capable in SQL, and fairly capable in Hibernate Criteria and HQL.</p>
  
  <p>I am trying to do a fairly simple select with an OR statement.</p>
  
  <p>In plain SQL my select looks like this:</p>
  
  <pre><code>SELECT * FROM CHANGED_LOG
  WHERE key1 = 52540 AND objectCode = 'Order'
  OR key1 = 48398 AND objectCode = 'Package'
  </code></pre>
  
  <p>This gives me every row where key1 = 52540 and objectCode equals Order
  AND also every row where key = 48398 and objectCode equals Package.
  This is exactly what i want.</p>
  
  <p>So trying to do this with JPA criteria (which seems exceptionally complicated...), my best guess so far has been this:</p>
  
  <pre><code>    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
  
      CriteriaQuery&lt;ChangedLogBean&gt; query = builder.createQuery(ChangedLogBean.class);
      Root&lt;ChangedLogBean&gt; from = query.from(ChangedLogBean.class);
      CriteriaQuery&lt;ChangedLogBean&gt; select = query.select(from);
  
      Predicate orderChangedLogBeans = builder.conjunction();
      builder.and(orderChangedLogBeans, builder.equal(from.get("key1"), orderId));
      builder.and(orderChangedLogBeans, builder.equal(from.get("objectCode"), ChangedLogBean.ObjectType.OrderBean));
  
      Predicate packageChangedLogBeans = builder.conjunction();
      builder.and(packageChangedLogBeans, builder.equal(from.get("key1"), packageId));
      builder.and(packageChangedLogBeans, builder.equal(from.get("objectCode"), ChangedLogBean.ObjectType.PackageBean));
  
      Predicate orderOrPackage = builder.disjunction();
      orderOrPackage.getExpressions().add(orderChangedLogBeans);
      orderOrPackage.getExpressions().add(packageChangedLogBeans);
  
      query.where(orderOrPackage);
  
      return entityManager.createQuery(select).getResultList();
  </code></pre>
  
  <p>Wow. A lot of lines for a simple query... But still, this returnes to me EVERY single row from the DB.</p>
  
  <p>What am i doing wrong here?</p>
  
  <p>Thanks for all your helpful answers :)</p>