title: Hibernate -- Criteria vs named query
body: <p>I'm trying to compare <a href="http://docs.jboss.org/hibernate/core/3.6/javadocs/org/hibernate/Criteria.html" rel="nofollow noreferrer">Hibernate Criteria</a> to named queries for performance. i'm aware it all depends on the actual query itself and the last word is on how they profile in runtime. still, trying to sort out what goes into each. </p>
  
  <p>i tried to organize the Q in two parts &amp; looking for verification/correction on both: </p>
  
  <p><strong>PART-1</strong> -- how Hibernate Criteria and named queries work basically:</p>
  
  <p>Criteria works on parameters. 
  In runtime, the query doesn`t need parsing-- has several search and "present-in-form" params like 
  order the results, return them as scrollable etc. 
  Haven't read/verified this, but Criteria works by 
  indexes on fields (based on the parameters set on them)
  to make it all faster. </p>
  
  <p>So, the advantage of Criteria compared to plain HQL 
  is its speed during execution. </p>
  
  <p>Named queries have the same advantage over HQL-- 
  the query is parsed once at start-up. Then executed 
  from wherever necessary in the application. </p>
  
  <p><strong>PART-2</strong> -- comparing the two: </p>
  
  <p>So in this picture, </p>
  
  <p>how do Criteria and named-queries compare to one another?</p>
  
  <p>Criteria works well for complicated queries across multiple tables 
  and multiple params-- has the means to optimize and thus make the query fast(?)</p>
  
  <p>named-queries have the advantage of "define-once-use-everywhere" and 
  are quite alright for "light" queries-- 
  less complicated searches with small amount of params typically on a single table. even better on frequent queries. </p>
  
  <p>Note: seen the very useful <a href="https://stackoverflow.com/questions/4401240/hibernate-criteria-vs-hql-which-is-faster">Hibernate Criteria vs HQL: which is faster?</a> among some other discussions. </p>
  
  <p>TIA. </p>