title: SQL query within hibernate extremely slow while very fast using SQL cient
body: <p>I know there have been quite a few similar questions but I cant find an answer to this one:</p>
  
  <p>I'm not very familiar with hibernate but I'm working on an app which contains an extremely slow hibernate query. This query is generated from a very simple query in the code:</p>
  
  <pre><code>Criteria crit = session.createCriteria(clazz); // clazz is the class of the Entities to load
  return crit.list();
  </code></pre>
  
  <p>There are probably a lot of possibility which could make this query extremey slow (hibernate bindings, drivers, etc, etc.) but what I dont understand is the following: I set hibernate to output the SQL statements it performs. The code above is translated into an awfully long query containing 3 inner joins and 9 left outer joins, the result being displayed in a table with around 80 columns. I'm sure, however, that only this single SQL statement is produced by hibernate and executed for this code.</p>
  
  <p>I'm using H2 database.</p>
  
  <ol>
  <li>When I run the code within the application, it takes around 20 seconds to execute (result set contains approx 6000 rows)</li>
  <li>If I code within the application a call to <code>Session.createSQLQuery(query).list()</code> where <code>query</code> is the sql generated by hibernate for the code above, it takes approx 30 sec to execute.</li>
  <li>When I execute the SQL query directly using H2 shell or even browser client, it takes less than a second to execute.</li>
  </ol>
  
  <p>What could be the reason(s) for such a huge difference between those 3 ways of executing the same query. I would expect an overhead with hibernate, but not by this amount.</p>