title: Will HQL query use Hibernate second-level cache
body: <p>I would like to clarify some points regarding the secondary level cache of hibernate. The point to clarify is, will the HQL queries always hit the database (at least for getting ids).</p>
  
  <p>Consider we have entities </p>
  
  <pre><code>class Customer {
  
      long id;  // Primary key
  
      String name;
  
      set &lt;Address&gt; addressList;   // One to many relationship
  
  }
  
  class Address{
  
      long id; // Primary key
  
      String houseName;
  
  }
  </code></pre>
  
  <p>The database table for the Address has a foreign key reference to the Customer (id) to support one to many relationship.</p>
  
  <p>As a precondition, I have enabled the level 2 cache for hibernate as EHcache. Only the entity and associations are set to be cacheable. Query caching is not enabled.</p>
  
  <p>I know that if I use the session.get() or session.load() more than one time, only the first call will fire a query to the databases and subsequent ones will take the data from level 2 cache.</p>
  
  <p>My questions are</p>
  
  <p>1) Will HQL take advantage of the second level cache. 
  In one session i executed one HQL to get the object using the primary key (id), "from Customer c where c.id = ? ").setParameter(1, 1005).</p>
  
  <p>If I ran the same HQL in a different session, will the Customer object is taken from the level 2 cache or it will hit the database again.</p>
  
  <p>2) Consider another HQL executed <code>from Customer as c left join fetch c.addressList</code> for selecting the customer and associated Address.</p>
  
  <p>If i ran the same HQL in a different session, will the associated Address be taken from the level 2 cache or it will hit the database again.</p>