问题描述
我使用带有 JPA2 的 Spring Boot 1.2.5 来注释实体(并且休眠作为底层 JPA 实现).
I'm using Spring Boot 1.2.5 with JPA2 to annotate entities (and hibernate as underlaying JPA implementation).
我想在那个设置中使用二级缓存,所以实体用 @javax.persistence.Cacheable
I wanted to use second level cache in that setup, so entities were annotated with @javax.persistence.Cacheable
我还在 application.properties 中添加了以下内容:
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
在启动 hibernate 期间抱怨缺少 EhCacheRegionFactory
所以我也把它添加到 pom:
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>
但是像 entityManager.find(Clazz.class, pk)
这样的查询仍然会触发数据库查询,而不是使用缓存数据.
But still queries like entityManager.find(Clazz.class, pk)
are firing DB query instead of using cached data.
知道缺少什么吗?
推荐答案
经过更多的挖掘,我在 application.properties
中遗漏了什么:
Well after some more digging here's what I was missing in application.properties
:
spring.jpa.properties.javax.persistence.sharedCache.mode=ALL
希望对某人有帮助:)
这篇关于Spring Boot + JPA2 + Hibernate - 启用二级缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!