注意Hibernate4在开发当中的一些改变
2019獨角獸企業重金招聘Python工程師標準>>>
注意Hibernate4在開發當中的一些改變
Hibernate4的改動較大只有spring3.1以上版本能夠支持,Spring3.1取消了HibernateTemplate,因為Hibernate4的事務管理已經很好了,不用Spring再擴展了。這里簡單介紹了hibernate4相對于hibernate3配置時出現的錯誤,只列舉了問題和解決方法,詳細原理如果大家感興趣還是去自己搜吧,網上很多。
Spring3.1去掉了HibernateDaoSupport類。hibernate4需要通過getCurrentSession()獲取session。并且設置
<prop?key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
(在hibernate3的時候是thread和jta)。
緩存設置改為<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>
Spring對hibernate的事務管理,不論是注解方式還是配置文件方式統一改為:
<bean?id="txManager"?class="org.springframework.orm.hibernate4.HibernateTransactionManager"?>??
<property?name="sessionFactory"><ref?bean="sessionFactory"/>
</property>?
</bean>
getCurrentSession()事務會自動關閉,所以在有所jsp頁面查詢數據都會關閉session。要想在jsp查詢數據庫需要加入:
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter過濾器。
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的傳播行為。
轉載于:https://my.oschina.net/u/1157912/blog/133059
總結
以上是生活随笔為你收集整理的注意Hibernate4在开发当中的一些改变的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于DBus的进程间通信(IPC)
- 下一篇: Spread for WinRT 7新功