title: Hibernate Query vs Criteria Performance
body: <p>I recently had a rather strange phenomenon. Had to obtain a count that included joins over multiple tables with different WHERE conditions. I implemented the query first with the criteria API of hibernate. It correctly created the requested prepared SQL statement but was rather slow. Re-implemented then the entire query using HQL. Was rather nasty to do that but the result performed much faster than with the Criteria API. Does anybody know the reason for that behavior? I assumed that the Criteria and HQL framework use the same code base to transform it to SQL.</p>
  
  <p>Here is the query:</p>
  
  <pre><code>select count(*) from R r where r.ISREPLACEDBY = 0 
  and r.STATUS='OK' and r.A = ? 
  and r.C in 
     (select distinct RC from CX cx where cx.FROMDATE &gt;= ? and cx.FROMDATE &lt;=?)
  </code></pre>