Java 休眠 NoCacheRegionFactoryAvailableException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19303348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:52:47  来源:igfitidea点击:

Hibernate NoCacheRegionFactoryAvailableException

javahibernatecaching

提问by

I'm getting a bizarre Hibernate exception that I can't explain. It's telling me that I'm using 2nd level cache, but no where in hibernate.cfg.xmldo I specify a 2nd level cache. Here's the exception:

我遇到了一个我无法解释的奇怪的 Hibernate 异常。它告诉我我正在使用二级缓存,但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.<init>(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)

And my hibernate.cfg.xml:

而我的hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- DataSource & Connection info. -->
        <property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
        <property name="hibernate.connection.driver.class">org.h2.Driver</property>
        <property name="hibernate.connection.url">jdbc:h2:file:/${MYAPP_HOME}/data/myapp</property>
        <property name="hibernate.connection.username">myapp</property>
        <property name="hibernate.connection.password">mypassword</property>
        <property name="hibernate.connection.pool_size">1</property>

        <!-- General Hibernate settings. -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="use_sql_comments">true</property>

        <!-- DDL Mode. -->
        <property name="hbm2ddl.auto">validate</property>

        <!-- All our Hibernate mapping XML files. -->
        <mapping class="net.me.myapp.common.dto.WordDTO" />
    </session-factory>
</hibernate-configuration>

Any ideas what would be triggering this exception? Thanks in advance!

任何想法会触发此异常?提前致谢!

采纳答案by Engineer

Pauwrote on hibernate.cache.region.factory_class Required in hibernate.cfg.xml:

Pauhibernate.cache.region.factory_class 上写道,在hibernate.cfg.xml 中需要

The exception is quite self-explanatory. You have to set the hibernate.cache.region.factory_classproperty. For instance with ehcache would be adding the following line:

<property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property>

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

<property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property>

回答by siebmanb

Using the following line fixed it :

使用以下行修复它:

<beans:entry key="hibernate.cache.use_second_level_cache" value="false"/>

But the Hibernate message is probably a warning that we should use second level cache?

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

回答by wilkejj

I also received this error and took me a while to track down. At one point we were going to have multiple cache regions, but in the end decided we were just going to have one cache pool.

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

When we merged that change into an older branch - we still had an entity with the old strategy of a cache pool per entity:

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

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

By removing the Cache annotation - it resolved the org.hibernate.cache.NoCacheRegionFactoryAvailableException:

通过删除缓存注释 - 它解决了 org.hibernate.cache.NoCacheRegionFactoryAvailableException:

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

Figured I'd post in case anyone else had a similar situation

想我会发帖以防其他人有类似的情况

回答by ashah

This error is very misleading, I spent almost a day to finally figure out the root cause. Thought my hibernate config file has defined second level cache and factory class, it was giving me error that hibernate.cache.region.factory_class is not given.

这个错误非常具有误导性,我花了将近一天的时间终于找出了根本原因。以为我的 hibernate 配置文件已经定义了二级缓存和工厂类,它给了我没有给出 hibernate.cache.region.factory_class 的错误。

I see that by hibernate.cfg.xml file is also available in classpath. But even after any change in that there was no impact and getting same error.

我看到通过 hibernate.cfg.xml 文件在类路径中也可用。但即使在任何变化之后也没有影响并出现相同的错误。

Finally I realized that for test purpose I have overridden persistence.xml file which has below missing properties under persistence-unit. After adding that issue was resolved.

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

 <properties>
            <property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml" />
</properties>

So this means my application was not able to find hibernate.cfg.xml file and somehow instead of giving error related to missing configuration, it cries out for factory class.

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

回答by Shubham Chopra

These are the property you need to add to enable second level cache

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

<!-- Provider for second level cache -->
        <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
        <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>