java 在 JBoss 4.2 上使用 JPA 启用 Hibernate 二级缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53562/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:51:46  来源:igfitidea点击:

Enabling Hibernate second-level cache with JPA on JBoss 4.2

javahibernatejpacaching

提问by Peter Hilton

What are the steps required to enable Hibernate's second-level cache, when using the Java Persistence API (annotated entities)? How do I check that it's working? I'm using JBoss 4.2.2.GA.

使用Java Persistence API(注解实体)时,启用Hibernate的二级缓存需要哪些步骤?我如何检查它是否有效?我正在使用 JBoss 4.2.2.GA。

From the Hibernate documentation, it seems that I need to enable the cache and specify a cache provider in persistence.xml, like:

从 Hibernate 文档来看,我似乎需要启用缓存并在persistence.xml 中指定一个缓存提供程序,例如:

<property name="hibernate.cache.use_second_level_cache"
          value="true" /> 
<property name="hibernate.cache.provider_class" 
          value="org.hibernate.cache.HashtableCacheProvider" /> 

What else is required? Do I need to add @Cacheannotations to my JPA entities?

还需要什么?我需要在JPA 实体中添加@Cache注释吗?

How can I tell if the cache is working? I have tried accessing cache statistics after running a Query, but Statistics.getSecondLevelCacheStatisticsreturns null, perhaps because I don't know what 'region' name to use.

如何判断缓存是否正常工作?我尝试在运行查询后访问缓存统计信息,但Statistics.getSecondLevelCacheStatistics返回 null,可能是因为我不知道要使用什么“区域”名称。

采纳答案by Tim Howland

I believe you need to add the cache annotations to tell hibernate how to use the second-level cache (read-only, read-write, etc). This was the case in my app (using spring / traditional hibernate and ehcache, so your mileage may vary). Once the caches were indicated, I started seeing messages that they were in use from hibernate.

我相信您需要添加缓存注释来告诉 hibernate 如何使用二级缓存(只读、读写等)。我的应用程序就是这种情况(使用 spring / 传统的休眠和 ehcache,所以你的里程可能会有所不同)。一旦显示缓存,我就开始看到它们正在从休眠状态使用的消息。

回答by Peter Hilton

Follow-up: in the end, after adding annotations, I have it working with EhCache, i.e.

后续:最后,添加注释后,我让它与EhCache一起工作,即

<property name="hibernate.cache.provider_class" 
          value="net.sf.ehcache.hibernate.EhCacheProvider" />