NoSuchMethodError: org.hibernate.SessionFactory.openSession

嚴重: Servlet.service() for servlet dispatcher threw exception
java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;
 at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:322)
 at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:256)

出問題嘍....

沒有這個方法?

哦...到hibernate4的時候,spring已經不再提供hibernateDaoSupport.getTemplate也不支持了.

就用hibernate4的session就可以了.

我把內容修改爲了

直接使用注入的sessionFactory.openSession();來取得session.看看行不行...

 

找到篇好文章,我之前遇到的問題都在這都能找到.其實出現這些問題的關鍵就是hibernate4和hibernate3出現了session管理的變動.spring也作出相應的變動....

錯誤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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章