title: hibernate 2nd level cache hits=0 for two successive identical queries
body: <p>Got a problem setting up 2nd level cache for hibernate for my Spring+Hibernate project or I don't understand how it works.</p>
  
  <p>I have one table roughly defined as this</p>
  
  <pre><code>@Entity
  @Cache(usage=CacheConcurrencyStrategy.READ_ONLY, region="testCache")
  public class TestClass extends MSId {
       @Transient
       STATUS statut = STATUS.NORMAL;
       public FamilleCatalogue(int id) {
            this.id = id;
       }
  }
  </code></pre>
  
  <p>In my configuration I have this :</p>
  
  <pre><code>        &lt;property name="hibernate.hbm2ddl.auto" value="update" /&gt;
          &lt;property name="hibernate.default_schema" value="dbtest" /&gt;
          &lt;property name="hibernate.cache.use_query_cache" value="false" /&gt;
          &lt;property name="hibernate.cache.use_second_level_cache" value="true" /&gt;
          &lt;property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory" /&gt;
          &lt;property name="hibernate.generate_statistics" value="true" /&gt;
          &lt;property name="hibernate.cache.generate_statistics" value="true" /&gt;
          &lt;property name="hibernate.cache.use_structured_entries" value="false" /&gt;
          &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /&gt;
          &lt;property name="hibernate.show_sql" value="false" /&gt;
          &lt;property name="hibernate.format_sql" value="false" /&gt;
          &lt;property name="hibernate.use_sql_comments" value="false" /&gt;
          &lt;property name="net.sf.ehcache.configurationResourceName" value="/spring/persistence/single/hibernate/ehcache.xml"/&gt;
          &lt;property name="hibernate.cache.use_reference_entries" value="true"/&gt;
  </code></pre>
  
  <p>my ehcache.xml contains :</p>
  
  <pre><code>&lt;cache
      name="testCache"
      eternal="false"
      timeToIdleSeconds="3600"
      timeToLiveSeconds="3600"
      statistics="true"
      logging="true"
      maxElementsInMemory="100"&gt;
  &lt;/cache&gt;
  </code></pre>
  
  <p>Now in my test I query all the element of my test class using this :</p>
  
  <pre><code>Query q = this.getEntityManager().createQuery("FROM " + entityClass.getSimpleName());
  List&lt;E&gt; result = q.getResultList();
  return result;
  </code></pre>
  
  <p>The thing is that I query 2 times the whole lot of them expecting that the second time the resulting object Will be taken from the 2nd level cache instead of the direct query in the database. However, each time I launch the query, all my entities are retrieved from the database and put in the cache as shown in the traces below :</p>
  
  <pre><code>======&gt; Query all test objects (1)
        --&gt; Entity Load Count                : 297
        --&gt; 2nd Level Hits                   : 1
        --&gt; 2nd Level Misses                 : 1
        --&gt; 2nd Level Puts                   : 296
  ======&gt; Query all test objects (2)
        --&gt; Entity Load Count                : 593
        --&gt; 2nd Level Hits                   : 1
        --&gt; 2nd Level Misses                 : 1
        --&gt; 2nd Level Puts                   : 592
  </code></pre>
  
  <ul>
  <li>Can anyone explain this ?</li>
  <li>Why does the number of puts gets incremented if the objects are already present in the cache ?</li>
  <li>Is it because 2nd level cache is only used when querying by ID ?</li>
  <li>Does this have to do with the QueryCache that I should enable ? because when I enable it, it's slower to retrieve datas from the query cache than to acctually query the database ...</li>
  </ul>
  
  <p>Precision : the hit=1 is the result of a Query by Id I launched on a specific object.</p>
  
  <p>Thanks for your help.</p>