Spring Boot + JPA2 + Hibernate - 启用二级缓存 [英] Spring Boot + JPA2 + Hibernate - enable second level cache

查看:601
本文介绍了Spring Boot + JPA2 + Hibernate - 启用二级缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JPA2的Spring Boot 1.2.5来注释实体(而hibernate是底层的JPA实现)。

我想使用二级缓存因此实体被注解为 @ javax.persistence.Cacheable



我还在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中:

 < dependency> 
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-ehcache< / artifactId>
< /依赖关系>

但是像 entityManager.find(Clazz.class,pk)正在触发数据库查询,而不是使用缓存数据。



任何想法缺少什么?

解决方案

在经过一些更深入的挖掘之后,我在 application.properties 中缺少了什么:

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

希望它能帮助某人:)

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?

解决方案

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 :)

这篇关于Spring Boot + JPA2 + Hibernate - 启用二级缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆