注意Hibernate4在開發當中的一些改變

來源:互聯網
上載者:User

標籤:c   class   java   http   ext   a   

Hibernate4的改動較大隻有spring3.1以上版本能夠支援,Spring3.1取消了HibernateTemplate,因為Hibernate4的交易管理已經很好了,不用Spring再擴充了。這裡簡單介紹了hibernate4相對於hibernate3配置時出現的錯誤,只列舉了問題和解決方案,詳細原理如果大家感興趣還是去自己搜吧,網上很多。

  1. Spring3.1去掉了HibernateDaoSupport類。hibernate4需要通過getCurrentSession()擷取session。並且設定
    <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
    (在hibernate3的時候是thread和jta)。

  2. 緩衝設定改為<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
                    <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>

  3. Spring對hibernate的交易管理,不論是註解方式還是設定檔方式統一改為:
    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" >  
    <property name="sessionFactory"><ref bean="sessionFactory"/>
    </property> 
    </bean>

  4. getCurrentSession()事務會自動關閉,所以在有所jsp頁面查詢資料都會關閉session。要想在jsp查詢資料庫需要加入:
    org.springframework.orm.hibernate4.support.OpenSessionInViewFilter過濾器。

  5. Hibernate分頁出現 ResultSet may only be accessed in a forward direction 需要設定hibernate結果集滾動
     <prop key="jdbc.use_scrollable_resultset">false</prop>

 

 

--------------------------------------------------------------------

找到篇好文章,我之前遇到的問題都在這都能找到。其實出現這些問題的關鍵就是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/hibernate/classic/Session;

        at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:324) [spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE]

        at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.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>

 

--------------------------------------------------------------------

由於Hibernate4已經完全可以實現事務了, 與Spring3.1中的hibernatedao,hibernateTemplete等有衝突,所以Spring3.1裡已經不提供Hibernatedaosupport,HibernateTemplete了,只能用Hibernate原始的方式用session:

        Session session = sessionFactory.openSession();
        Session session = sessionFactory.getCurrentSession();
在basedao裡可以用注入的sessionFactory擷取session.

注意, 配置事務的時候必須將父類baseServiceImpl也配上,要不然會出現錯誤:No Session found for currentthread, 以前是不需要的

SessionFactory.getCurrentSession()的後台實現是可拔插的。因此,引入了新的擴充介面 (org.hibernate.context.spi.CurrentSessionContext)和

新的配置參數(hibernate.current_session_context_class),以便對什麼是“當前session”的範圍和上下文(scope and context)的定義進行拔插。

 

Spring @Transactional聲明式交易管理,”currentSession”的定義為: 當前被 Spring交易管理員 管理的Session,此時應配置:

hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext。

 

此處一定注意 使用 hibernate4,在不使用OpenSessionInView模式時,在使用getCurrentSession()時會有如下問題: 當有一個方法list 傳播行為為Supports,當在另一個方法getPage()(無事務)調用list方法時會拋出org.hibernate.HibernateException: No Session found for current thread 異常。 這是因為getCurrentSession()在沒有session的情況下不會自動建立一個,不知道這是不是Spring3.1實現的bug。 因此最好的解決方案是使用REQUIRED的傳播行為

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.