title: Understanding Hibernate's Criteria#setMaxResults
body: <p>The <a href="http://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/Criteria.html#setMaxResults(int)" rel="noreferrer">docs</a> say:</p>
  
  <pre><code>Criteria setMaxResults(int maxResults)
  &gt;Set a limit upon the number of objects to be retrieved.
  </code></pre>
  
  <p>Let's say I have the following <code>Criteria</code>:</p>
  
  <pre><code>Criteria criteria = createCriteria(); // creates criteria for MY_TABLE entity
  criteria.list().length; // let's say there's a million records in this table
  </code></pre>
  
  <p>Will adding <code>criteria.setMaxResults(1)</code> return only a single row? Or, will it still return 1 million rows, but pick one of them?</p>
  
  <p>When running a query similar to my above code example against my Oracle DB, I saw that <code>... ROWNUM &lt; 2</code> SQL was being generated.</p>
  
  <p>But, when I looked at a <a href="http://www.orafaq.com/wiki/ROWNUM" rel="noreferrer">ROWNUM FAQ</a>, I didn't understand if <code>ROWNUM</code> will be applied <strong>before</strong> retrieving records, or after. I would expect "before" to be efficient, whereas "after" would not be for a large result set.</p>
  
  <blockquote>
    <p>ROWNUM is evaluated AFTER records are selected from the database and
    BEFORE the execution of ORDER BY clause.</p>
  </blockquote>