2 yonghuxiaoying yonghuxiaoying 于 2017.09.12 15:04 提问

关于springMVC+hibernate4+eacache二级缓存配置问题,启动一直报错

1、已经成功导入了相关jar包:hibernate-core 4.3.8Final;hibernate-ehcache 4.3.8Final和ehcache2.9.0
2、配置如下
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">



${hibernate.hbm2ddl.auto}
${hibernate.dialect}
${hibernate.show_sql}
${hibernate.format_sql}
none
<!-- 开启查询的二级缓存 如果不需要就不设置 -->

true
<!-- 开启二级缓存 ehcache -->
true

<!-- Hibernate4.0以上设置factory -->

org.hibernate.cache.ehcache.EhCacheRegionFactory
<!-- 二级缓存 ehcache的配置文件位置 -->
classpath:configs/ehcache.xml



其中也注意了hibernate4和3的区别了
3、在实体类以标签注入@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)后,启动项目会报错
4、错误如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'riskalertDao': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory_riskalert' defined in class path resource [configs/spring-data-config.xml]: Invocation of init method failed; nested exception is org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given; please either disable second level cache or set correct region factory using the hibernate.cache.region.factory_class setting and make sure the second level cache provider (hibernate-infinispan, e.g.) is available on the classpath.
······
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory_riskalert' defined in class path resource [configs/spring-data-config.xml]: Invocation of init method failed; nested exception is org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given; please either disable second level cache or set correct region factory using the hibernate.cache.region.factory_class setting and make sure the second level cache provider (hibernate-infinispan, e.g.) is available on the classpath.
······
Caused by: org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given; please either disable second level cache or set correct region factory using the hibernate.cache.region.factory_class setting and make sure the second level cache provider (hibernate-infinispan, e.g.) is available on the classpath.
······
九月 12, 2017 2:58:18 下午 org.apache.catalina.core.StandardContext listenerStart
严重: Exception sending context initialized event to listener instance of class com.prosas.basic.utils.ProjectInit
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'riskalertDao': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory_riskalert' defined in class path resource [configs/spring-data-config.xml]: Invocation of init method failed; nested exception is org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given; please either disable second level cache or set correct region factory using the hibernate.cache.region.factory_class setting and make sure the second level cache provider (hibernate-infinispan, e.g.) is available on the classpath.
······
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory_riskalert' defined in class path resource [configs/spring-data-config.xml]: Invocation of init method failed; nested exception is org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given; please either disable second level cache or set correct region factory using the hibernate.cache.region.factory_class setting and make sure the second level cache provider (hibernate-infinispan, e.g.) is available on the classpath.
······
Caused by: org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given; please either disable second level cache or set correct region factory using the hibernate.cache.region.factory_class setting and make sure the second level cache provider (hibernate-infinispan, e.g.) is available on the classpath.
······
5、网上查了很多资料,都说这个错误是org.hibernate.cache.ehcache.EhCacheRegionFactory这行有问题,但是说的解决方法中都是改成这样啊,就是解决不了问题很苦恼,求大神解答!

3个回答

yonghuxiaoying
yonghuxiaoying   2017.09.12 15:06

在线等大神来帮忙啊·································

bingyan_wxl
bingyan_wxl   2017.09.12 15:29
Hibernate4的配置是下面这样的,你仔细检查一下是不是这样配的

    <property name="hibernate.cache.use_second_level_cache">true</property>  
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>  
<property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</property>  
bingyan_wxl
bingyan_wxl 可能是tomcat有缓存吧
2 个月之前 回复
yonghuxiaoying
yonghuxiaoying 回复bingyan_wxl: 我又尝试了一次 加上后又不报错了 我这次彻底晕了
2 个月之前 回复
bingyan_wxl
bingyan_wxl 回复火舞天涯: 刚才又查了一下资料,可能是jar包版本问题,你把ehcache的版本改成2.4.3试一下
2 个月之前 回复
yonghuxiaoying
yonghuxiaoying 回复bingyan_wxl: 哦
2 个月之前 回复
bingyan_wxl
bingyan_wxl 回复火舞天涯: 好吧,那我就不知道了,我也是从网上查的,没用过hibernate
2 个月之前 回复
yonghuxiaoying
yonghuxiaoying 我网查了很多都是不加第三条的,不过我也试过加上了 但是依然报错
2 个月之前 回复
yonghuxiaoying
yonghuxiaoying   2017.09.12 18:36

没有大神来看看吗???还是没有解决 刚才缓存问题导致成功了 白高兴一场

Csdn user default icon
上传中...
上传图片
插入图片