Announcement Announcement Module
Collapse
No announcement yet.
Spring 3 and Hibernate 4 Migration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 3 and Hibernate 4 Migration

    Hello,
    I'm started to migrate a legacy application to the new versions of theses libraryries.
    The libs:
    /app/WebContent/WEB-INF
    /app/WebContent/WEB-INF/lib
    /app/WebContent/WEB-INF/lib/activation.jar
    /app/WebContent/WEB-INF/lib/antlr-2.7.7.jar
    /app/WebContent/WEB-INF/lib/axis.jar
    /app/WebContent/WEB-INF/lib/brazilutils-0.1.1.jar
    /app/WebContent/WEB-INF/lib/com.springsource.org.aopalliance-1.0.0.jar
    /app/WebContent/WEB-INF/lib/commons-collections-3.2.1.jar
    /app/WebContent/WEB-INF/lib/commons-dbcp-1.4.jar
    /app/WebContent/WEB-INF/lib/commons-fileupload-1.2.2.jar
    /app/WebContent/WEB-INF/lib/commons-logging-api-1.1.1.jar
    /app/WebContent/WEB-INF/lib/commons-pool-1.5.7.jar
    /app/WebContent/WEB-INF/lib/cssparser-0.9.6.jar
    /app/WebContent/WEB-INF/lib/dom4j-1.6.1.jar
    /app/WebContent/WEB-INF/lib/ehcache-core-2.4.3.jar
    /app/WebContent/WEB-INF/lib/ejb3-persistence.jar
    /app/WebContent/WEB-INF/lib/el-api-2.2.0-SNAPSHOT.jar
    /app/WebContent/WEB-INF/lib/el-impl-2.2.0-SNAPSHOT.jar
    /app/WebContent/WEB-INF/lib/fontes.jar
    /app/WebContent/WEB-INF/lib/groovy-all-1.7.5.jar
    /app/WebContent/WEB-INF/lib/guava-11.0.jar
    /app/WebContent/WEB-INF/lib/hibernate-annotations-3.5.6-Final.jar
    /app/WebContent/WEB-INF/lib/hibernate-commons-annotations-4.0.1.Final.jar
    /app/WebContent/WEB-INF/lib/hibernate-core-4.0.1.Final.jar
    /app/WebContent/WEB-INF/lib/hibernate-ehcache-4.0.1.Final.jar
    /app/WebContent/WEB-INF/lib/hibernate-entitymanager-4.0.1.Final.jar
    /app/WebContent/WEB-INF/lib/hibernate-jpa-2.0-api-1.0.1.Final.jar
    /app/WebContent/WEB-INF/lib/hibernate-validator-4.2.0.Final.jar
    /app/WebContent/WEB-INF/lib/iText-2.1.7.jar
    /app/WebContent/WEB-INF/lib/jasperreports-4.0.0.jar
    /app/WebContent/WEB-INF/lib/javassist-3.15.0-GA.jar
    /app/WebContent/WEB-INF/lib/javax.faces-2.1.4.jar
    /app/WebContent/WEB-INF/lib/jaxrpc.jar
    /app/WebContent/WEB-INF/lib/jaybird-full-2.1.6.jar
    /app/WebContent/WEB-INF/lib/jboss-logging-3.1.0.CR2.jar
    /app/WebContent/WEB-INF/lib/jboss-transaction-api_1.1_spec-1.0.0.Final.jar
    /app/WebContent/WEB-INF/lib/jsf-api-2.1.6.jar
    /app/WebContent/WEB-INF/lib/jsf-impl-2.1.6.jar
    /app/WebContent/WEB-INF/lib/jsf-tlds.jar
    /app/WebContent/WEB-INF/lib/jta-1.1.jar
    /app/WebContent/WEB-INF/lib/log4j-1.2.16.jar
    /app/WebContent/WEB-INF/lib/mail.jar
    /app/WebContent/WEB-INF/lib/ojdbc6.jar
    /app/WebContent/WEB-INF/lib/org.springframework.aop-3.1.0.RELEASE.jar
    /app/WebContent/WEB-INF/lib/org.springframework.asm-3.1.0.RELEASE.jar
    /app/WebContent/WEB-INF/lib/org.springframework.beans-3.1.0.RELEASE.jar
    /app/WebContent/WEB-INF/lib/org.springframework.context-3.1.0.RELEASE.jar
    /app/WebContent/WEB-INF/lib/org.springframework.core-3.1.0.RELEASE.jar
    /app/WebContent/WEB-INF/lib/org.springframework.expression-3.1.0.RELEASE.jar
    /app/WebContent/WEB-INF/lib/org.springframework.jdbc-3.1.0.RELEASE.jar
    /app/WebContent/WEB-INF/lib/org.springframework.orm-3.1.0.RELEASE.jar
    /app/WebContent/WEB-INF/lib/org.springframework.transaction-3.1.0.RELEASE.jar
    /app/WebContent/WEB-INF/lib/org.springframework.web-3.1.0.RELEASE.jar
    /app/WebContent/WEB-INF/lib/quartz-1.8.3.jar
    /app/WebContent/WEB-INF/lib/richfaces-components-api-4.1.0.Final.jar
    /app/WebContent/WEB-INF/lib/richfaces-components-ui-4.1.0.Final.jar
    /app/WebContent/WEB-INF/lib/richfaces-core-api-4.1.0.Final.jar
    /app/WebContent/WEB-INF/lib/richfaces-core-impl-4.1.0.Final.jar
    /app/WebContent/WEB-INF/lib/saaj.jar
    /app/WebContent/WEB-INF/lib/sac.jar
    /app/WebContent/WEB-INF/lib/slf4j-api-1.5.6.jar
    /app/WebContent/WEB-INF/lib/slf4j-simple-1.5.6.jar
    /app/WebContent/WEB-INF/lib/spring-annotation-base-1.1.1.GA.jar
    /app/WebContent/WEB-INF/lib/spring-annotation-hibernate-1.1.1.GA.jar
    /app/WebContent/WEB-INF/lib/spring-annotation-web-1.1.1.GA.jar
    /app/WebContent/WEB-INF/lib/sqljdbc4.jar
    /app/WebContent/WEB-INF/lib/standard.jar
    /app/WebContent/WEB-INF/lib/webserviceutils.jar
    /app/WebContent/WEB-INF/lib/wsdl4j.jar
    Application-context.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:util="http://www.springframework.org/schema/util" 
           xmlns:aop="http://www.springframework.org/schema/aop" 
           xmlns:sa="http://sannotations.sourceforge.net/context"
           xsi:schemaLocation="
              http://sannotations.sourceforge.net/context         http://sannotations.sourceforge.net/context.xsd
              http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
              http://www.springframework.org/schema/aop           http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
              http://www.springframework.org/schema/util          http://www.springframework.org/schema/util/spring-util-2.0.xsd
              http://www.springframework.org/schema/tx            http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
        default-autowire="byName">
     
        <tx:annotation-driven transaction-manager="transactionManager" />
    
    	<bean id="applicationContextProvider" scope="singleton" lazy-init="false"  class="br.com.app.prog.util.ApplicationContextProviderBean">
        </bean>
    	
    	<bean id="propertyConfigurer"
    		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="location">
    			<value>file:/appDS/dbxconnections.properties</value>
    		</property>
    	</bean> 
    	    
        <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" >
            <property name="basenames">
                <list>
                    <value>br.com.app.prog.messages.StringResources</value>
                </list>
            </property>
        </bean>
    	    
    	<bean id="dataSourceapp" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    		<property name="defaultTransactionIsolation" value="2" />
    		<property name="driverClassName" value="${dbxconnections.driverClassName}" />
            <property name="url" value="${dbxconnections.url}" />
            <property name="username" value="${dbxconnections.username}" />
            <property name="password" value="${dbxconnections.password}" />
            
    		<property name="maxActive" value="50" />
    		<property name="logAbandoned" value="true" />
    		<property name="validationQuery" value="SELECT * FROM USUARIOS" />
    		<property name="testOnBorrow" value="true" />
    	</bean>
    
        <bean id="sessionFactory"
            class="net.sourceforge.sannotations.hibernate.AutomaticAnnotationSessionFactoryBean">
            <property name="dataSource" ref="dataSourceapp" />
            <property name="schemaUpdate" value="false" />
            <property name="entityInterceptor" ref="auditLogInterceptor"/>
            
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">${dbxconnections.dialect}</prop>
                    <prop key="hibernate.show_sql">${dbxconnections.show_sql}</prop>
                    <prop key="hibernate.transaction.flush_before_completion">${dbxconnections.flush_before_completion}</prop>
                    <prop key="hibernate.current_session_context_class">${dbxconnections.current_session_context_class}</prop>
                    <prop key="hibernate.bytecode.use_reflection_optimizer">${dbxconnections.use_reflection_optimizer}</prop>
                </props>
            </property>
        </bean>
    
        <bean id="transactionManager"
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSourceapp" />
        </bean>
    
    	<bean id="jdbcTemplate"
    		  class="org.springframework.jdbc.core.JdbcTemplate">
    		<property name="dataSource" ref="dataSourceapp" />
        </bean>
        
    	<bean id="auditLogInterceptor" class="br.com.app.prog.bean.AuditoriaInterceptorBean" />
        
      	...
    	
    	<sa:annotation-autoload />
    
    </beans>
    continues...
    Last edited by phew; Jan 16th, 2012, 04:42 AM.

  • #2
    continuing...


    ERROR:
    Code:
    EVERE: Context initialization failed
    org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean] for bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean
    	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1262)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:576)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1331)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:897)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:566)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384)
    	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
    	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4323)
    	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4780)
    	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
    	at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:988)
    	at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:771)
    	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
    	at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:988)
    	at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:275)
    	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
    	at org.apache.catalina.core.StandardService.startInternal(StandardService.java:427)
    	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
    	at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:649)
    	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
    	at org.apache.catalina.startup.Catalina.start(Catalina.java:585)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:415)
    Caused by: java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean
    	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1666)
    	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1511)
    	at org.springframework.util.ClassUtils.forName(ClassUtils.java:257)
    	at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:417)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1283)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1254)
    	... 29 more
    14/01/2012 16:43:30 org.apache.catalina.core.StandardContext listenerStart
    SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean] for bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean
    	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1262)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:576)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1331)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:897)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:566)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384)
    	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
    	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4323)
    	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4780)
    	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
    	at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:988)
    	at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:771)
    	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
    	at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:988)
    	at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:275)
    	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
    	at org.apache.catalina.core.StandardService.startInternal(StandardService.java:427)
    	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
    	at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:649)
    	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
    	at org.apache.catalina.startup.Catalina.start(Catalina.java:585)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:415)
    Caused by: java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean
    	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1666)
    	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1511)
    	at org.springframework.util.ClassUtils.forName(ClassUtils.java:257)
    	at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:417)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1283)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1254)
    	... 29 more
    How is the better way to upgrade my code?

    Comment


    • #3
      Have you actually READ the stack trace and READ the reference guide?

      For hibernate4 there is no AnnotationSessionFactoryBean there is only a LocalSessionFactoryBean.

      Also your configuration seems flawed.

      1) You are using hibernate but are using a DataSourceTransactionManager (why?) you should use the HibernateTransactionManager which is also perfectly capable of managing JDBC transactions.
      2) NEVER mess around with the current_session_context property of hibernate UNLESS you use JTA (but you aren't using that).

      Comment


      • #4
        You've mentioned the reference guide, but I can't find information about the Hibernate4 support in it. If anyone could show how they configure all the classes, would be great. It stopped working when I moved to Hibernate 4 even though I updated packages to hibernate4 and removed the exception translator. Looks like something changed with datasources and hibernate properties, but that's just my guess.
        Last edited by ctapobep; Feb 23rd, 2012, 02:07 PM.

        Comment


        • #5
          From my point of view, it was quite easy to move to hibernate4.

          Yes the AnnotationSessionFactoryBean has been replace for hibernate4 to LocalSessionFactoryBean with (almost) the same parameters. Even HibernateExceptionTranslator is almost the same.

          I even feel like the opening of session is better working with transactionmanager and @Transactional.

          Code:
          	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
          		p:packagesToScan="com.company" 
          		p:dataSource-ref="xxxDB"
          		/>
          
          
           	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory"/>
          
          	<!-- Translates Hibernate exceptions to Spring Data Access Exceptions -->
          	<bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator"/>

          Comment


          • #6
            setSchemaUpdate

            Originally posted by cleclefl View Post
            ...
            Yes the AnnotationSessionFactoryBean has been replace for hibernate4 to LocalSessionFactoryBean with (almost) the same parameters.
            ...
            I have also tried to migrate but method setSchemaUpdate is missing on LocalSessionFactoryBean which I used on AnnotationSessionFactoryBean. How do I get the tables created in my database after migration? (I do not use this in production environment.)

            Best Regards
            smasseman

            Comment


            • #7
              hibenate4 and spring 3Integration

              1)Instead of AnnotationSessionFactoryBean you can use "LocalSessionFactoryBuilder" as AnnotationSessionFactoryBean is removed from the Hibernate4.1.0

              2)also if you use the current_session_context_class in your hibernate.cfg.xml please remove or comment out that property..................

              3)why not u use HibernateTransactionManager instead of DataSourceTransactionManager

              Comment

              Working...
              X