测试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 morehibernate.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文件吗,我没配置。

解决方案 »

  1.   

    <property name="cache.provider_class">org.hibernate.cache.internal.EhCacheProvider</property>
    改为
    <property name="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory 
    </property>
      

  2.   


    试过的,错误还是一样。org.hibernate.cache.EhCacheRegionFactory  类包里没有,我倒的包好像有问题
      

  3.   

    hibernate4.x之后修改了这个,但是文档自带的配置文件并没有更正,我一开始也遇到了这个问题。<property name="cache.provider_class">org.hibernate.cache.internal.EhCacheProvider</property>
    改成:
    <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
      

  4.   

    修改之后测试成功了,可是为什么要这样改呢?默认设置和4.1的文档里面都是<property name="cache.provider_class">org.hibernate.cache.internal.EhCacheProvider</property>
      

  5.   

    <!-- <property name="cache.use_second_level_cache">true</property> -->我把这一句注释掉了,好像也没问题,不知道是不是我哪里配错了?
    其他代码如下:
    @Entity
    @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
    public class Student {……//junit4测试代码如下
    @Test
    public void testCache2() {
    Session ss = HibernateUtil.getSession();
    ss.beginTransaction();
    Student s = (Student) ss.load(Student.class, 2);
    System.out.println(s.getName());
    ss.getTransaction().commit(); Session ss2 = HibernateUtil.getSession();
    ss2.beginTransaction();
    Student s2 = (Student) ss2.load(Student.class, 2);
    System.out.println(s2.getName());
    ss2.getTransaction().commit();
    }