如何使用JPA2的@Cacheable而不是Hibernate的@Cache

通常,我使用Hibernate的@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)来caching一个@Entity类,它运行良好。

在JPA2中,还有另外一个@Cacheable注释,看起来和Hibernate的@Cache有相同的function。 为了让我的实体类独立于Hibernate的包,我想试试看。 但是我不能使它工作。 每次简单的ID查询仍然击中数据库。

谁能告诉我哪里出了问题? 谢谢。

实体类:

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

testing课:

 @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)); } } 

testing结果显示每个“get”命中数据库层(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代码:

 @Override public T get(Serializable id) { return em.find(clazz, id); } 

根据JPA 2.0规范,如果要使用@Cacheable批注有select地caching实体,则应该在persistence.xml指定<shared-cache-mode> (或等效的javax.persistence.sharedCache.mode当创buildEntityManagerFactory )。

下面是一个具有相关元素和属性的示例persistence.xml

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

请注意,我看过至less有一个与caching相关的问题HHH-5303 。 所以上面不保证:)

参考

  • Hibernate EntityManager参考指南
    • 2.2.1包装
  • JPA 2.0规范
    • 第3.7.1节“共享高速caching模式元素”
    • 第11.1.7节“可caching注释”

对于那些使用Springconfiguration而不是persistence.xml ,下面是一个示例:

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

另请注意,如果使用@Cacheable批注,则只能使用由RegionFactorygetDefaultAccessType()方法确定的默认caching并发策略。 在EhCache的情况下它是READ_WRITE 。 如果你想使用另一种策略,你必须使用Hibernate的@Cache标注。