title: Criteria API combined AND / OR
body: <p>I am struggling to create a query using the Criteria API. The following SQL query returns the result that I need:</p>
  
  <pre><code>SELECT * FROM MODEL WHERE MANUFACTURER_ID = 1 AND SHORTNAME LIKE '%SF%' OR LONGNAME LIKE '%SF%';
  </code></pre>
  
  <p>I have written the following code using the criteria API:</p>
  
  <pre><code>public List&lt;Model&gt; findAllByManufacturer(Manufacturer manufacturer,
          String pattern) {
      CriteriaBuilder cb = em.getCriteriaBuilder();
      CriteriaQuery&lt;Model&gt; cq = cb.createQuery(Model.class);
      Root&lt;Model&gt; m = cq.from(Model.class);
      cq.select(m);
      Join&lt;Model, Manufacturer&gt; mf = m.join("manufacturer");
      Predicate p = cb.equal(mf.get("id"), manufacturer.getId());
      p = cb.and(cb.like(cb.upper(m.&lt;String&gt;get("shortName")),
              pattern.toUpperCase()));
      p = cb.or(cb.equal(cb.upper(m.&lt;String&gt;get("longName")),
              pattern.toUpperCase()));
      cq.where(p);
      return em.createQuery(cq).getResultList();
  }
  </code></pre>
  
  <p>When I think about it logically, it should work:</p>
  
  <pre><code>Predicate p = cb.equal(mf.get("id"), manufacturer.getId());
  </code></pre>
  
  <p>WHERE MANUFACTURER_ID = 1</p>
  
  <pre><code>p = cb.and(cb.like(cb.upper(m.&lt;String&gt;get("shortName")),
                  pattern.toUpperCase()));
  </code></pre>
  
  <p>AND SHORTNAME LIKE '%SF%'</p>
  
  <pre><code>p = cb.or(cb.equal(cb.upper(m.&lt;String&gt;get("longName")),
                  pattern.toUpperCase()));
  </code></pre>
  
  <p>OR LONGNAME LIKE '%SF%'</p>
  
  <p>The query does properly run in that I am not getting any errors, it just is not returning any results. Any clue on where I went wrong are very much appreciated!</p>