日期:2014-05-19  浏览次数:20662 次

hibernate 二级缓存如何配置
测试hibeinate的二级缓存,hibernate 4.1.1 Myeclipse 8.6.出现以下异常:

at com.hsp.view.TestMain.main(TestMain.java:28)
Caused by: org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_classis not gaven, please either disable second level cache or set correct region factory class name to property hibernate.cache.region.factory_class (and make sure the second level cache provider, hibernate-infinispan, for example, available in the classpath).
at org.hibernate.cache.internal.NoCachingRegionFactory.buildEntityRegion(NoCachingRegionFactory.java:69)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:347)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1740)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1778)
at com.hsp.util.HibernateUtil.<clinit>(HibernateUtil.java:23)
... 1 more

hibernate.cfg.xml 如下:
<hibernate-configuration>

<session-factory>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="connection.url">
jdbc:oracle:thin:@127.0.0.1:1521:orcl
</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="myeclipse.connection.profile">
myoralce
</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>

<!-- 配置二级缓存 ,启用二级缓存-->
<property name="cache.use_second_level_cache">true</property>
<property name="cache.use_query_cache">true</property>
<!-- 指定使用哪种二级缓存 -->
<property name="cache.provider_class">org.hibernate.cache.internal.EhCacheProvider</property>
<mapping resource="com/hsp/domain/Department.hbm.xml" />
<mapping resource="com/hsp/domain/Student.hbm.xml" />

<class-cache class="com.hsp.domain.Student" usage="read-write"/>
</session-factory>

</hibernate-configuration>

我发现org.hibernate.cache.internal包中并没有EhCacheProvider这个类,是这个原因导致的吗?还有需要配置ehcache.xml文件吗,我没配置。

------解决方案--------------------
<property name="cache.provider_class">org.hibernate.cache.internal.EhCacheProvider</property>
改为
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory 
</property>