简体   繁体   English

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

[英]Enabling Hibernate second-level cache with JPA on JBoss 4.2

What are the steps required to enable Hibernate's second-level cache, when using the Java Persistence API (annotated entities)? 使用Java Persistence API(带注释的实体)时,启用Hibernate的二级缓存需要哪些步骤? How do I check that it's working? 我该如何检查它是否正常工作? I'm using JBoss 4.2.2.GA. 我正在使用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 @Cache annotations to my JPA entities? 我是否需要将@Cache注释添加到JPA实体中?

How can I tell if the cache is working? 如何判断缓存是否正常? I have tried accessing cache statistics after running a Query, but Statistics.getSecondLevelCacheStatistics returns null, perhaps because I don't know what 'region' name to use. 我在运行Query之后尝试访问缓存统计信息,但Statistics.getSecondLevelCacheStatistics返回null,可能是因为我不知道要使用什么'region'名称。

Follow-up: in the end, after adding annotations, I have it working with EhCache, ie 后续:最后,在添加注释后,我让它与EhCache一起工作,即

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

I believe you need to add the cache annotations to tell hibernate how to use the second-level cache (read-only, read-write, etc). 我相信你需要添加缓存注释来告诉hibernate如何使用二级缓存(只读,读写等)。 This was the case in my app (using spring / traditional hibernate and ehcache, so your mileage may vary). 这是我的应用程序中的情况(使用弹簧/传统的休眠和ehcache,所以你的里程可能会有所不同)。 Once the caches were indicated, I started seeing messages that they were in use from hibernate. 一旦指示了缓存,我就开始看到它们正在使用来自休眠的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM