Hibernate NoCacheRegionFactoryAvailableException

我得到了一个奇怪的Hibernateexception,我无法解释。 它告诉我,我正在使用二级缓存,但在hibernate.cfg.xml没有指定二级缓存。 这是例外:

 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 class name to property hibernate.cache.region.factory_class (and make sure the second level cache provider, hibernate-infinispan, for example, is available in the classpath). at org.hibernate.cache.internal.NoCachingRegionFactory.buildEntityRegion(NoCachingRegionFactory.java:69) at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:348) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1769) at net.me.myapp.common.dao.SessionFactoryProvider.newSessionFactory(SessionFactoryProvider.java:37) at net.me.myapp.common.dao.BaseDAO.doPersist(BaseDAO.java:28) at net.me.myapp.common.dao.WordDAO.deleteAllWords(WordDAO.java:36) at net.me.myapp.tools.dmapper.DictionaryMapper.run(DictionaryMapper.java:88) at net.me.myapp.tools.dmapper.DictionaryMapper.main(DictionaryMapper.java:56) 

我的hibernate.cfg.xml

      org.hibernate.dialect.H2Dialect org.h2.Driver jdbc:h2:file:/${MYAPP_HOME}/data/myapp myapp mypassword 1  true true true  validate     

什么会触发这个例外? 提前致谢!

Pau写的hibernate.cache.region.factory_class在hibernate.cfg.xml中是必需的 :

例外是不言自明的。 您必须设置hibernate.cache.region.factory_class属性。 例如,使用ehcache将添加以下行:

 net.sf.ehcache.hibernate.EhCacheRegionFactory 

使用以下行修复它:

  

但Hibernate消息可能是一个警告,我们应该使用二级缓存?

我也收到了这个错误并花了一段时间追查。 有一次,我们将有多个缓存区域,但最终我们决定只有一个缓存池。

当我们将该更改合并到较旧的分支时 – 我们仍然有一个实体具有每个实体的缓存池的旧策略:

 @Entity @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region="tableRegion") @Table(name = "table") public class table { 

通过删除Cache注释 – 它解决了org.hibernate.cache.NoCacheRegionFactoryAvailableException:

 @Entity @Table(name = "table") public class table { 

如果其他人有类似的情况,我想发布

这个错误是非常误导的,我花了差不多一天才弄清楚根本原因。 以为我的hibernate配置文件定义了二级缓存和工厂类,它给了我错误,没有给出hibernate.cache.region.factory_class。

我看到hibernate.cfg.xml文件在classpath中也可用。 但即使经过任何改变,也没有任何影响并得到同样的错误。

最后我意识到,出于测试目的,我已经覆盖了persistence.xml文件,该文件在persistence-unit下具有以下缺少的属性。 添加后,该问题已解决。

     

所以这意味着我的应用程序无法找到hibernate.cfg.xml文件,并以某种方式而不是提供与缺少配置相关的错误,它呼吁工厂类。

这些是您需要添加以启用二级缓存的属性

  org.hibernate.cache.EhCacheProvider org.hibernate.cache.ehcache.EhCacheRegionFactory