Hibernate 4.2使用Ehcache二级缓存时的异常NoCacheRegionFactoryAvailableException-解决方案

(xjl456852整理)

Hibernate版本4.2.2
Encache版本2.4.3

错误hibernate.cfg.xml配置:

<property name="cache.use_second_level_cache">true</property>
<property name="cache.provider_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<property name="hibernate.cache.use_query_cache">true</property>

加入了Encache目录下的jar文件,结果出现下面的异常信息:

[main] ERROR com.ryan.HibernateUtil - Initial SessionFactory creation failed: 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).


hibernate文档中并没有说明hibernate.cache.region.factory_classis应该怎么配置,所以出现了问题,hibernate文档写的真不好.找了很长时间才找到了解决方案,需要对hibernate.cfg.xml配置修改一下.

正确的hibernate.cfg.xml配置:

<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.use_query_cache">true</property>

这样写就没有问题了,hibernate二级缓存就可配置成功了

你可能感兴趣的:(二级缓存,hibernate4配置)