16

I found some instructions how to configure pure hibernate to use EHCache. But I can't find any instructions how to configure JPA2.0 EntityManager to use cache. Hibernate 3.5.2 is my JPA2.0 provider.

edit// Is @Cacheable(true) enough for entity? Or should I use @org.hibernate.annotations.Cache to configure the entity?

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
Piotr Gwiazda
  • 12,080
  • 13
  • 60
  • 91
  • 1
    Related question [How to use JPA2's @Cacheable instead of Hibernate's @Cache](http://stackoverflow.com/questions/3663979/how-to-use-jpa2s-cacheable-instead-of-hibernates-cache) – Pascal Thivent Sep 09 '10 at 09:17

2 Answers2

30

I found some instructions how to configure pure hibernate to use EHCache. But I can't find any instructions how to configure JPA2.0 EntityManager to use cache. Hibernate 3.5.2 is my JPA2.0 provider.

The way you configure the L2 cache provider with JPA is similar is similar to raw Hibernate.

By default, Hibernate 3.5 ships with EhCache 1.5 (see Configure Ehcache as a Second Level Cache) and if you want to use the official cache provider provided by Hibernate (in hibernate-ehcache if you are using Maven), declare:

<!-- This is the provider for Ehcache provided by Hibernate, using the "old" SPI -->
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>

If you want to use EhCache 2.x, you'll need to use the provider provided by EhCache which supports the new Hibernate 3.3/3.5 SPI with its CacheRegionFactory). Use:

<!-- The region factory property is the "new" property (for Hibernate 3.3 and above) -->
<property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory">

for instance creation, or

<property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory"/>

to force Hibernate to use a singleton of Ehcache CacheManager.

And then activate L2 caching and query caching:

<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>

That's for the Hibernate L2 cache setup.

Is @Cacheable(true) enough for entity? Or should I use @org.hibernate.annotations.Cache to configure the entity?

In theory, the @Cacheable is supposed to be a replacement for the Hibernate proprietary annotation and should be used in conjunction with the shared-cache-mode element:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
  <persistence-unit name="FooPu" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    ...
    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
    <properties>
      ...
    </properties>
  </persistence-unit>
</persistence>

But as mentioned in this previous question, initial experimentation has not been successful (it might be related to HHH-5303, I can't say, I didn't investigate that much). So I suggest sticking with the proprietary annotations.

References

  • Hibernate EntityManager reference guide
  • JPA 2.0 Specification
    • Section 3.7.1 "The shared-cache-mode Element"
    • Section 11.1.7 "Cacheable Annotation"

Resources

Related question

Community
  • 1
  • 1
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
3

in persistence.xml you can specify this property:

<property name="hibernate.cache.region.factory_class"
       value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" />

and to make it active:

<property name="hibernate.cache.use_second_level_cache" value="true" />
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • What happened with class org.hibernate.cache.EhCacheProvider doesn't exist in hibernate 3.5.2 – Piotr Gwiazda Sep 09 '10 at 08:34
  • download EhCache separately and put it on your classpath. the Value in my answer is not "org.hibernate.." – Bozho Sep 09 '10 at 08:55
  • With Hibernate 4.1 all the above configuration not working . getting java.lang.NoClassDefFoundError: org/hibernate/cache/TimestampsRegion this was there in Hibernate 3 but removed in Hibernate 4. Any comments ? – R-JANA Feb 06 '13 at 10:32