2

I am using spring 3.2 and hibernate 4.2.8 and tried using ehcache but the moment I annotate an entity class with @Cache(org.hibernate.annotations.Cache) i get the following exception

Caused by: 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:351)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1797)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1868)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:247)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:373)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:358)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1547)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1485)
    ... 42 more

My Configuration is as shown below

<bean id="sessionFactoryAdmin"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="adminDataSource" />
        <property name="packagesToScan" value="com.digilegal.services.ahc.model.user" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
                <prop key="hibernate.cache.provider_configuration">/WEB-INF/ehcache-entity.xml</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
            </props>
        </property>
    </bean>

I have tried all possible links on the internet but could not solve the problem

Here is my /WEB-INF/ehcache-entity.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache>
    <cache name="com.digilegal.services.ahc.model.user.UserNotifications"
        maxElementsInMemory="500"
        eternal="false"
        overflowToDisk="false"
        timeToIdleSeconds="69"
        timeToLiveSeconds="65" />


    <cache name="org.hibernate.cache.StandardQueryCache"
        maxEntriesLocalHeap="15" eternal="false" timeToLiveSeconds="60"
        overflowToDisk="true" />
</ehcache>

Thanks ​Nirav

13
  • Could you share your /WEB-INF/ehcache-entity.xml. It could be incorrect or you have missed that Commented Apr 11, 2014 at 18:25
  • Also verify whether you have the ehcache library on your classpath. Using maven <artifactId>hibernate-ehcache</artifactId>. The exception is thrown in situations when the ehcache lib is not on the classpath. Commented Apr 11, 2014 at 18:31
  • Well I think /WEB-INF/ehcache-entity.xml is in the classpath . Since when I did not mention /WEB-INF it gave me an error Commented Apr 11, 2014 at 18:35
  • The ehcache lib should be in the path. If you are using maven put this into the pom.xml <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>4.3.0.Final</version> </dependency> Commented Apr 11, 2014 at 18:36
  • I am using following maven dependency for ehcache<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>4.3.4.Final</version> </dependency> Commented Apr 11, 2014 at 18:36

1 Answer 1

11

I had this error recently, and in my case the error was in a test that was using a different session factory configuration to where no hibernate.cache.region.factory_class was specified.

So the presence of the @Cache annotation caused the test to abort with the exact same error. In my case i did not want caching for that particular session factory so I added:

<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>

have a look at your config to see if you don't have multiple session factories defined, and some don't provide hibernate.cache.region.factory_class.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much this did the trick, I had multiple session factory and had to disable hibernate second level cache in the other one .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.