spring3.1.1整合hibernate4.1.2出现的问题

错误1:java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider
原因:spring的sessionfactory和transactionmanager与支持hibernate3时不同。
解决:
 

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        ...
    </bean>

    
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>



错误2:java.lang.NoSuchMethodError:
org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session
原因:hibernate4之后,spring31把HibernateDaoSupport去除,包括数据访问都不需要hibernatetemplate,这意味着dao需要改写,直接使用hibernate的session和query接口。
解决:为了改写dao,足足花了一天时间,然后一个个接口进行单元测试,这是蛋疼的一个主要原因。

错误3:nested exception is org.hibernate.HibernateException: No Session found for current thread
原因:发现一些bean无法获得当前session,需要把之前一些方法的事务从NOT_SUPPORT提升到required,readonly=true
见https://jira.springsource.org/browse/SPR-9020
http://www.iteye.com/topic/1120924
解决:
 

<tx:advice id="baseServiceAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true" propagation="REQUIRED"/><!--之前是NOT_SUPPORT-->
            <tx:method name="find*" read-only="true" propagation="REQUIRED"/><!--之前是NOT_SUPPORT-->
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="remove*" propagation="REQUIRED"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <!--默认其他方法都是REQUIRED-->
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>



错误4:与错误3报错类似,java.lang.NoSuchMethodError:org.hibernate.SessionFactory.openSession()Lorg/hibe
rnate/classic/Session;
        at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(S
essionFactoryUtils.java:324) [spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE]
        at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(Ses
sionFactoryUtils.java:202) [spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE]
        at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

原因:因为opensessioninview filter的问题,如果你的配置还是hibernate3,需要改为hibernate4
 

        <filter>
          <filter-name>openSessionInViewFilter</filter-name>
          <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
        </filter>

猜你喜欢

转载自ligaosong.iteye.com/blog/1597427