Friday, December 27, 2013

Initial SessionFactory creation failed: org.hibernate.cache.NoCacheRegionFactoryAvailableException:

Initial SessionFactory creation failed:  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:346)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1754)
   at com.ranga.util.HibernateUtil.<clinit>(HibernateUtil.java:17)
   at com.ranga.App4.main(App4.java:12)
Exception in thread "main" java.lang.ExceptionInInitializerError
   at com.ranga.util.HibernateUtil.<clinit>(HibernateUtil.java:20)
   at com.ranga.App4.main(App4.java:12)

Problem: You are not provided or specified hibernate.cache.region.factory property in configuration file.

Solution:  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">
org.hibernate.cache.internal.EhCacheRegionFactory
</property>

4.0 configured as follows:
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class"> org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
3.3 configured as follows:
<property name="hibernate.cache.use_second_level_cache"> true </ property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider
</ property>

Complete two cache configuration:
Configuration before copying a file to the src folder under ehcache.xml
hibernate.cfg.xml settings:
  1. <property name="hibernate.cache.use_second_level_cache">true</ property>
<property name="hibernate.cache.region.factory_class"> org.hibernate.cache.ehcache.EhCacheRegionFactory </ property>
  1. @ Cache annotation (extensions provided by the hibernate)    
@ Cache (usage = CacheConcurrencyStrategy.READ_WRITE)

Need to add in the pom.xml
<dependency>
         <groupId>org.hibernate</groupId>
         <artifactId>hibernate-ehcache</artifactId>
         <version>4.1.12.Final</version>
       </dependency>

Otherwise it will error:
Unable to load class [org.hibernate.cache.ehcache.EhCacheRegionFactory]

0 comments: