0

I`m upgrading project from Spring-4.1.6.RELEASE to Spring-4.2.1.RELEASE and Hibernate (core and Ehcache) from 4.3.9.Final to 5.0.1.Final.

In Spring-4.1.6.RELEASE, there is a method

org.springframework.orm.hibernate4.LocalSessionFactoryBean.setCacheRegionFactory(RegionFactory cacheRegionFactory)

But in Spring 4.2.1.RELEASE, there is no such method in class

org.springframework.orm.hibernate5.LocalSessionFactoryBean

So when I set property in hibernate session factory bean to enable second level caching using

<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>

Following error is thrown:

WARN : org.springframework.beans.factory.support.DefaultListableBeanFactory - Bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in class path resource [application-context.xml]: Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [application-context.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [application-context.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'cacheRegionFactory' of bean class [org.springframework.orm.hibernate5.LocalSessionFactoryBean]: Bean property 'cacheRegionFactory' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
WARN : org.springframework.web.context.support.XmlWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.envers.spring.dao.UserDAO com.envers.spring.HomeController.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.envers.spring.dao.UserDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in class path resource [application-context.xml]: Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [application-context.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [application-context.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'cacheRegionFactory' of bean class [org.springframework.orm.hibernate5.LocalSessionFactoryBean]: Bean property 'cacheRegionFactory' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:667)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:633)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:681)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:552)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:493)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1231)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1031)

How to configure second level caching in Spring-4.2.1.RELEASE and Hibernate-5.0.1.FINAL? What will be the code substitution in Hibernate-5 to enable second level caching?

Please help on this migration issue. Thanks in advanced.

Nirav Patel
  • 1,304
  • 2
  • 13
  • 30

1 Answers1

0

tl;dr version: Change HibernateTransactionManager class's package from org.springframework.orm.hibernate4 to org.springframework.orm.hibernate5.

I was able to get this working by changing from this:

<!-- Set up default hibernate properties. -->
<bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="properties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
        </props>
    </property>
</bean>

<bean id="cacheRegionFactory" class="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory">
    <constructor-arg index="0" ref="hibernateProperties" />
</bean>

<bean id="sessionFactory" class="org.nrg.framework.orm.hibernate.AggregatedAnnotationSessionFactoryBean"
      p:cacheRegionFactory-ref="cacheRegionFactory" p:dataSource-ref="dataSource"
      p:hibernateProperties-ref="hibernateProperties" p:namingStrategy-ref="namingStrategy" />

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" />

To this:

<!-- Set up default hibernate properties. -->
<bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="properties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
        </props>
    </property>
</bean>

<bean id="sessionFactory" class="org.nrg.framework.orm.hibernate.AggregatedAnnotationSessionFactoryBean"
      p:dataSource-ref="dataSource" p:hibernateProperties-ref="hibernateProperties" p:physicalNamingStrategy-ref="namingStrategy" />

<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" />

The big changes here are:

<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>

You've already done this part.

The other changes are:

  • Changing p:namingStrategy-ref to p:physicalNamingStrategy-ref (this required a change in my custom naming implementation, but pretty easy)
  • Removing the SingletonEhCacheRegionFactory bean declaration
  • Removing the p:cacheRegionFactory-ref property setting
  • Changing the HibernateTransactionManager class's package from org.springframework.orm.hibernate4 to org.springframework.orm.hibernate5

I think the last one is probably the critical thing you need to change.

Overall, I'd prefer to have the type-safe ability to set the cache region manager through a bean rather than the free-form property value setting, but this will patch us over until I can figure out another way to do it.

Spanky Quigman
  • 870
  • 7
  • 16
  • I had changed hibernate TransactionManager version from 4 to 5 but issue is due to org.hibernate.cache.ehcache.EhCacheRegionFactory class. If I use org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory then it is working properly. I don`t know what changes are required to use EhCacheRegionFactory. – Nirav Patel Nov 17 '15 at 10:25