How to use JPA2's @Cacheable instead of Hibernate's @Cache

55,110

Solution 1

According to the JPA 2.0 specification, if you want to selectively cache entities using the @Cacheable annotation, you're supposed to specify a <shared-cache-mode> in the persistence.xml (or the equivalent javax.persistence.sharedCache.mode when creating the EntityManagerFactory).

Below, a sample persistence.xml with the relevant element and properties:

<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>
      ...
      <property name="hibernate.cache.provider_class" value="org.hibernate.cache.SingletonEhCacheProvider"/>
      <property name="hibernate.cache.use_second_level_cache" value="true"/>
      <property name="hibernate.cache.use_query_cache" value="true"/>
    </properties>
  </persistence-unit>
</persistence>

Note that I've seen at least one issue HHH-5303 related to caching. So the above is not guaranteed :)

References

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

Solution 2

For those who use Spring config instead of persistence.xml, here is a sample:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="MYSQL"/>
            <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
            <property name="showSql" value="true"/>
            <property name="generateDdl" value="false"/>
        </bean>
    </property>
    <property name="packagesToScan" value="com.mycompany.myproject.domain"/>
    <property name="jpaPropertyMap">
        <map>
            <entry key="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"/>
            <entry key="hibernate.cache.use_second_level_cache" value="true"/>
            <entry key="hibernate.cache.use_query_cache" value="true"/>
            <entry key="javax.persistence.sharedCache.mode" value="ENABLE_SELECTIVE" />
        </map>
    </property>
</bean>

Also note that if you're using @Cacheable annotations, you can only use a default cache concurrency strategy, which is determined by the getDefaultAccessType() method of the RegionFactory. In case of EhCache it's READ_WRITE. If you want to use another strategy, you have to use Hibernate's @Cache annotations.

Share:
55,110
smallufo
Author by

smallufo

A Java/Kotlin programmer twitter : http://twitter.com/smallufo

Updated on July 09, 2022

Comments

  • smallufo
    smallufo almost 2 years

    Typically , I use Hibernate's @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) to cache an @Entity class , and it works well.

    In JPA2 , there's another @Cacheable annotation that seems to be the same functionality with Hibernate's @Cache. To make my entity class independent of hibernate's package , I want to give it a try. But I cannot make it work. Each time a simple id query still hits the DB.

    Can anybody tell me where goes wrong ? Thanks.

    Entity class :

    @Entity
    //@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    @Cacheable(true) 
    public class User implements Serializable
    {
     // properties
    }
    

    Test class :

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"classpath:app.xml"})
    @TransactionConfiguration(transactionManager="transactionManager")
    public class UserCacheTest
    {
      @Inject protected UserDao userDao;
    
      @Transactional
      @Test
      public void testGet1()
      {
        assertNotNull(userDao.get(2L));
      }
    
      @Transactional
      @Test
      public void testGet2()
      {
        assertNotNull(userDao.get(2L));
      }
    
      @Transactional
      @Test
      public void testGet3()
      {
        assertNotNull(userDao.get(2L));
      }
    }
    

    The test result shows each "get" hits DB layer (with hibernate.show_sql=true).

    Persistence.xml :

    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
    <property name="hibernate.show_sql" value="true"/>
    <property name="hibernate.format_sql" value="true" />
    <property name="hibernate.use_outer_join" value="true"/>
    
    <property name="hibernate.cache.provider_class" value="org.hibernate.cache.SingletonEhCacheProvider"/>
    <property name="hibernate.cache.use_second_level_cache" value="true"/>
    <property name="hibernate.cache.use_query_cache" value="true"/>
    

    JPA code :

    @Override
    public T get(Serializable id)
    {
      return em.find(clazz, id);
    }
    
  • smallufo
    smallufo over 13 years
    Thank you , and it seems still not working in 3.5.5-Final. Anyway , I'll keep an eye on HHH-5303. Thanks.
  • David Tinker
    David Tinker over 13 years
    These instructions worked for me using 3.5.5-Final and ehcache 2.3.1
  • Scarlett
    Scarlett about 10 years
    what is <entry key="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFac‌​tory"/> used for? will it use hibernate's cache?
  • John29
    John29 about 10 years
    @Scarlett It specifies which caching implementation Hibernate will use for the second level cache. It’s described here.
  • Bilbo Baggins
    Bilbo Baggins over 9 years
    It may not work with 3.5.5 because I think JPA 2.0 is supported by hibernate 4.0 it self.
  • cuter44
    cuter44 over 7 years
    NOTE: Since 4.0 on, it is possible to config default strategy globally, without depending on hibernate package in code, see docs.jboss.org/hibernate/orm/4.0/devguide/en-US/html/ch06.ht‌​ml