title: If/Case statement in JPA Criteria Builder
body: <p>I want to build up a query which search dates on different entites. My structure is:</p>
  
  <ul>
  <li>Contract has date (non nullable)</li>
  <li>Employee has date (non nullable)</li>
  <li>Employee may have a contract id (nullable)</li>
  </ul>
  
  <p>If a Employee has a contract I want to retrieve the contract date. If an employee does not have a contract then I want to return the Employee date. </p>
  
  <p>My code so far is:</p>
  
  <pre><code>if (inputDate!= null) {
      ParameterExpression&lt;Date&gt; exp = criteriaBuilder.parameter(Date.class, "inputDate");
      criteria.add(criteriaBuilder.or(
          criteriaBuilder.isNull(employee.get("contract")),
          criteriaBuilder.lessThanOrEqualTo(employee.&lt;Date&gt;get("creationDate"), exp),   criteriaBuilder.lessThanOrEqualTo((employee.join("contract").&lt;Date&gt;get("fromDate")), exp) ));}
  </code></pre>
  
  <p>This does not seem to work though. I always appear to go into the isNull which I do not expect.</p>
  
  <p>I am happy to look into this some more but I guess my question is whether this is the correct way of going about it. Is it? I have seen a selectCase as well in criteriaBuilder so perhaps that may be a better solution.</p>
  
  <p>Any pointers would be greatly received.</p>
  
  <p>Thanks</p>