title: Having clause along with 'case when' in CriteriaBuilder
body: <p>I want to build the having clause shown below using <code>CriteriaBuilder</code> :</p>
  
  <pre class="lang-sql prettyprint-override"><code>select objectid,
     sum(case when attr_meta = 'severity' then 1 else 0 end) as severity,
     sum(case when attr_meta = 'priority' then 1 else 0 end) as priority
  from object d
  group by objectid
  having sum(case when attr_meta = 'severity' then 1 else 0 end) != 1 
      or sum(case when attr_meta = 'priority' then 1 else 0 end) != 1;
  </code></pre>
  
  <p>I tried the below approach:</p>
  
  <pre><code>Predicate p = cb.equal(cb.sum(cb.&lt;Integer&gt;selectCase()
      .when(cb.equal(root.get("name"), 'severity'), 1).otherwise(0)), 1);
  p = cb.or(p, cb.equal(cb.sum(cb.&lt;Integer&gt;selectCase()
      .when(cb.equal(root.get("name"), 'priority'), 1).otherwise(0)), 1));
  </code></pre>
  
  <p>but this gives the below exception:</p>
  
  <pre><code>java.lang.NullPointerException
      at java.lang.Class.isAssignableFrom(Native Method)
      at org.hibernate.ejb.criteria.ValueHandlerFactory.isNumeric(ValueHandlerFactory.java:70)
      at org.hibernate.ejb.criteria.predicate.ComparisonPredicate.&lt;init&gt;(ComparisonPredicate.java:69)
      at org.hibernate.ejb.criteria.CriteriaBuilderImpl.equal(CriteriaBuilderImpl.java:392)
  </code></pre>
  
  <p>The exception seems to be coming from the outer <code>CriteriaBuilderImpl.equal()</code> call, the one that encloses <code>selectCase()</code> call.</p>
  
  <p>The <code>equal()</code> call internally needs type info of the <code>Expression</code>. The <code>selectCase()</code> call, while creating the <code>Expression</code>, puts in type as <code>null</code>. Do we have some way to handle this situation? Either a way to let the <code>equal()</code> know the type, or a completely different approach to the above mentioned query?</p>