EDIT: I should state that I've been researching this for a couple of days now - there is plenty of information on "how to configure ehcache with Hibernate" but they mostly do not refer to using JPA and annotations - they either refer to pure Hibernate or to configuration through XML. (Just want to make it clear that I have already been around the internet on this problem.) I'm using JPA and annotations, so most of these configuration guides refer to files I don't have in my app (like hbm.xml files).
I have an app that is using Hibernate 3.6.10.FINAL, Spring Data 1.3.2.RELEASE, and Spring version 3.2.1.RELEASE. I'm trying to get the second-level caching working in hibernate. According to the documentation, I can do that simply by including the following dependency and configuration:
(POM.XML)
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate.version}</version>
</dependency>
(PERSISTENCE.XML)
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider" />
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.generate_statistics" value="true" />
I've annotated one of my entity classes using the javax.persistence.Cacheable annotation and tried to view the statistics in a JUnit test:
public void cacheTest() {
RandomDataGenerator randomData = new RandomDataGenerator();
for (int i = 0; i < 10; i++) {
AppMaster master = masterService.findOne(randomData.nextLong(1, 10));
logger.debug(String.format("Read one record back = %1$d, %2$s", master.getApplicationId(), master.getApContact()),AppMasterServiceTest.class);
// Get statistics from hibernate session
Session session = (Session)masterService.getEntityManager().getDelegate();
Statistics statistics = session.getSessionFactory().getStatistics();
logger.debug(String.format("Second level stats = %1$d, %2$d, %3$d",
statistics.getSecondLevelCachePutCount(),
statistics.getSecondLevelCacheHitCount(),
statistics.getSecondLevelCacheMissCount()), AppMasterServiceTest.class);
}
But the statistics appear to always be zero.
2013-11-11 11:20:33,908 DEBUG [main] test.service.AppMasterServiceTest - Second level stats = 0, 0, 0
Can anyone help me diagnose this?