简体   繁体   中英

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)? How do I check that it's working? I'm using 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:

<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?

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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