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!
Pau wrote on hibernate.cache.region.factory_class Required in hibernate.cfg.xml:
These are the property you need to add to enable second level cache
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:
By removing the Cache annotation - it resolved the org.hibernate.cache.NoCacheRegionFactoryAvailableException:
Figured I'd post in case anyone else had a similar situation
Using the following line fixed it :
But the Hibernate message is probably a warning that we should use second level cache?
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.
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.