1. 程式人生 > >hibernate4 和 spring3 整合注意事項:HibernateDaoSupport沒有了找到篇好文章,我之前遇到的問題都在這都能找到。其實出現這些問題的關鍵就是hibernate4和hib

hibernate4 和 spring3 整合注意事項:HibernateDaoSupport沒有了找到篇好文章,我之前遇到的問題都在這都能找到。其實出現這些問題的關鍵就是hibernate4和hib

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的傳播行為

總之:spring對hibernate的session長度的管理於是事物關係在一起,當事物結束的時候session關閉,當事物中斷或異常的時候session關閉,此種方式比在容器初始化的時(

<filter>

    <filter-name>openSessionInViewFilter</filter-name>

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

</filter>

)候開啟session效能要好,