Hibernate NoCacheRegionFactoryAvailableException

2020-07-05 05:48发布

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.xml do I specify a 2nd level cache. Here's the exception:

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:

<?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!

5条回答
Ridiculous、
2楼-- · 2020-07-05 06:20

Pau wrote on hibernate.cache.region.factory_class Required in hibernate.cfg.xml:

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

<property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property>
查看更多
走好不送
3楼-- · 2020-07-05 06:20

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>
查看更多
对你真心纯属浪费
4楼-- · 2020-07-05 06:28

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:

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

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

查看更多
我命由我不由天
5楼-- · 2020-07-05 06:39

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?

查看更多
我命由我不由天
6楼-- · 2020-07-05 06:39

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.

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.

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.

 <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.

查看更多
登录 后发表回答