Java Ehcache 二级缓存不适用于 JPA 和 Hibernate?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19913168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ehcache second level cache not working with JPA and Hibernate?
提问by user1071914
EDIT:I should state that I've been researching this for a couple of days now - there is plentyof 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).
编辑:我应该声明我已经研究了几天了 - 有很多关于“如何使用 Hibernate 配置 ehcache”的信息,但它们大多不指使用 JPA 和注释 - 它们要么指的是纯休眠或通过 XML 进行配置。(只是想说明我已经在互联网上解决了这个问题。)我正在使用 JPA 和注释,所以这些配置指南中的大部分都引用了我的应用程序中没有的文件(如 hbm.xml文件)。
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:
我有一个使用 Hibernate 3.6.10.FINAL、Spring Data 1.3.2.RELEASE 和 Spring 版本 3.2.1.RELEASE 的应用程序。我试图让二级缓存在休眠状态下工作。根据文档,我可以通过包含以下依赖项和配置来简单地做到这一点:
(POM.XML)
(POM.XML)
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate.version}</version>
</dependency>
(PERSISTENCE.XML)
(持久性.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:
我已经使用 javax.persistence.Cacheable 注释注释了我的实体类之一,并尝试在 JUnit 测试中查看统计信息:
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?
谁能帮我诊断一下?
采纳答案by Planky
javax.persistence.Cachable requires you set ENABLE_SELECTIVE in your persistence.xml. You should include a line like the following:
javax.persistence.Cachable 要求您在persistence.xml 中设置ENABLE_SELECTIVE。您应该包括如下一行:
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
Check out the docs on http://docs.oracle.com/javaee/6/api/javax/persistence/Cacheable.html
查看http://docs.oracle.com/javaee/6/api/javax/persistence/Cacheable.html上的文档
Also, take a look at this related question: How to use JPA2's @Cacheable instead of Hibernate's @Cache
另外,看看这个相关的问题:How to use JPA2's @Cacheable instead of Hibernate's @Cache