title: Hibernate 2nd Level caching doesnt seem to be working
body: <p>Im currently trying to get hibernate working using the caching provider that comes with hibernate. </p>
  
  <pre><code>net.sf.ehcache.hibernate.SingletonEhCacheProvider
  </code></pre>
  
  <p>I have a default cache and a class specific cache enabled in the ecache.xml which is referenced in my hibernate.cfg.xml file. The class/mapping file specific cache is defined to handle upto 20000 objects.</p>
  
  <p>However, I'm seeing no perfrormance gains since I turned on the cache mapping on one of the mapping files Im testing this with.</p>
  
  <p>My test is as follows.</p>
  
  <p>Load 10000 objects of the particular mapping file im testing (this should hit the DB and be a bottle neck).
  Next I go to load the same 10000 objects, as this point I would expect the cache to be hit and to see significant performance gains.  Have tried using both "read-only" and "read-write" cache mapping on the hibernate mapping xml file Im testing with.</p>
  
  <p>I'm wondering is their anything I need to be doing to ensure the cache is being hit before the DB when loading objects? </p>
  
  <p>Note as part of the test im pagin through these 10000 records using something similar to below ( paging a 1000 records at time).</p>
  
  <pre><code>Criteria crit = HibernateUtil.getSession() .createCriteria( persistentClass );
         crit.setFirstResult(startIndex);
         crit.setFetchSize(fetchSize);
         return crit.list();
  </code></pre>
  
  <p>Have seen that criteria has a caching mode setter ( <code>setCacheMode()</code> ) so is there something I should be doing with that?? </p>
  
  <p>I notice using the below stats code that theres 10000 objects (well hiberante dehydrated onjects i imagine??)  in memory but 
  for some reason I'm getting 0 hits and more worryingly 0 misses so it looks like its not going to the cache at all when its doing a look up even though the stats code seems to be telling me that theres 10000 objects in memory.</p>
  
  <p>Any ideas on what im doing worng? I take it the fact im getting misses is good as it means the cache is being used, but i cant figure out why im not getting any cache hits. Is it down to the fact im using <code>setFirstResult()</code> and <code>setFetchSize()</code> with criteria.</p>
  
  <pre><code>System.out.println("Cache Misses = " + stats.getSecondLevelCacheMissCount());
  System.out.println("Cache Hits Count = " + stats.getSecondLevelCacheHitCount());
  
  System.out.println("2nd level elements in mem "+ stats.getSecondLevelCacheStatistics("com.SomeTestEntity").getElementCountInMemory());
  </code></pre>