hibernatespring-bootjpa-2.0second-level-cache

Spring Boot + JPA2 + Hibernate - enable second level cache


I'm using Spring Boot 1.2.5 with JPA2 to annotate entities (and hibernate as underlaying JPA implementation).

I wanted to use second level cache in that setup, so entities were annotated with @javax.persistence.Cacheable

I also added following in application.properties:

spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.use_query_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory

During bootup hibernate complained about lack of EhCacheRegionFactory so I also added this to pom:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-ehcache</artifactId>
</dependency>

But still queries like entityManager.find(Clazz.class, pk) are firing DB query instead of using cached data.

Any idea what is missing?


Solution

  • Well after some more digging here's what I was missing in application.properties:

    spring.jpa.properties.javax.persistence.sharedCache.mode=ALL
    

    Hope it helps someone :)