在使用spring3和hibernate4.2集成與hibernate3有很多的不同,其中之一就是spring3不在支持HibernateTemplate,而是使用hibernate原生的api,我在集成的時候遇到了如下兩個問題。?
問題之一:在使用session.save()方法保存數據時不能成功的保存到數據庫
?
??? 這個問題的原因是在獲取session時,不能使用openSession()方法,而要使用getCurrentSession()方法?
- ??????
- @Resource (name= "sf" )??
- private ?SessionFactory?sessionFactory;???
- Session?session?;??
- ...??
- session?=?sessionFactory.getCurrentSession();??
問題之二:使用getCurrentSession時報:hibernate4 org.hibernate.HibernateException: No Session的錯誤
?
??? 這個問題的原因是session未打開,解決方式:?
??? 前提記得在service層上面加上@Transaction注釋,否則任然會有相同的異常?
方式1-在web.xml中加過濾器?
- <!--?open?session?filter?-->??
- <filter>??
- ????<filter-name>openSessionInViewFilter</filter-name>??
- ????<filter- class >org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter- class >??
- ????<init-param>??
- ????<param-name>singleSession</param-name>??
- ????<param-value> true </param-value>??
- ????</init-param>??
- </filter>??
方式2-配置事務的切面?
- <!--?用spring管理事務?-->??
- ???<bean?id= "transactionManager" ? class = "org.springframework.orm.hibernate4.HibernateTransactionManager" >??????
- ???????<property?name= "sessionFactory" ?ref= "sf" />??????
- ???</bean>????
- ?????
- ???<!--?配置使用注解的方式來使用事務?-->???
- <tx:annotation-driven?transaction-manager= "transactionManager" ?/>??
- ??
- <!--?配置那些類的方法進行事務管理,需要aopalliance- 1.0 .jar和aspectjweaver.jar,當前com.neusoft.leehom.service包中的子包,????
- ??????????????????????類中所有方法需要,還需要參考tx:advice的設置?-->????
- ??
- ???<!--?這是事務通知操作,使用的事務管理器引用自?transactionManager?-->????
- ???<tx:advice?id= "txAdvice" ?transaction-manager= "transactionManager" >????
- ???????<tx:attributes>????
- ??
- ???????????<tx:method?name= "insert*" ?propagation= "REQUIRED" ?/>????
- ???????????<tx:method?name= "update*" ?propagation= "REQUIRED" ?/>????
- ???????????<tx:method?name= "delete*" ?propagation= "REQUIRED" ?/>????
- ???????????<tx:method?name= "get*" ?propagation= "REQUIRED" ?read-only= "true" />????
- ???????????<tx:method?name= "query*" ?propagation= "REQUIRED" ?read-only= "true" />????
- ???????????<tx:method?name= "*" ?propagation= "REQUIRED" ?/>????
- ???????</tx:attributes>????
- ???</tx:advice>???
- ?????
- ????<!--?需要引入aop的命名空間?-->????
- ???<aop:config>????
- ???????<!--?切入點指明了在執行Service的所有方法時產生事務攔截操作?-->????
- ???????<aop:pointcut?id= "daoMethods" ?expression= "execution(*?com.tl..serviceimpl.*.*(..))" ?/>????????
- ???????<!--?定義了將采用何種攔截操作,這里引用到?txAdvice?-->????
- ???????<aop:advisor?advice-ref= "txAdvice" ?pointcut-ref= "daoMethods" ?/>????
- ???</aop:config> ??
在使用Spring mvc + Spring + Hibernate4 配置 的時候,總是出現?No Session found for current thread,仔細檢查applicationContext.xml和dispacter-servlet.xml文件,注解、事務 配置 都沒有 問題 ,糾結好久。
網上搜了很多方法,都不能 解決 。
有說加上 < prop ? key = "hibernate.current_session_context_class" > thread </ prop >的 配置 。 有說hibernate4要加上的是 < prop key = "hibernate.current_session_context_class" > org.springframework.orm.hibernate4.SpringSessionContext </ prop >,經過測試,都不能 解決 。
看了http://blog.csdn.net/qq445422083/article/details/8160387帖子之后,覺得說的有道理,還是事務沒有裝配。
? ? ? ? 試著把 dispacter-servlet.xml中的內容合并到 applicationContext.xml中,采用一個 配置 文件(當然web.xml也要做相應修改),發現并沒有報錯, 問題解決 。
? ? ? ? 但是自己還是喜歡使用兩個 配置 文件,這樣結構更清晰。
? ? ? ? 分析為什么合并到一起就沒 問題 呢,原來 spring的context是父子容器,所以會產生沖突,Controller會首先被掃描裝配,而此時的Service還沒有進行事務的 配置 ,獲得的將是原樣的Service(沒有經過事務裝配,故而沒有事務處理能力) ,最后才是applicationContext.xml中的掃描設備進行事務處理。
這樣就好辦了,讓兩個 配置 文件各干各的事就可以了。
1 、在Spring主容器中(applicationContext.xml),用 < context:exclude-filter/> 將Controller的注解過濾掉,不掃描裝配它。
?
- < context:component-scan ? base-package = "com" > ??
- ?? < context:exclude-filter ? type = "annotation" ? expression = "org.springframework.stereotype.Controller" ? /> ??
- </ context:component-scan > ???
2 而在springMVC 配置 文件中(dispatcher-servlet.xml)將Service和Dao的注解給 過濾 掉 ,只掃描裝配 Controller。
?
?
- < context:component-scan ? base-package = "com" > ??
- ?? < context:include-filter ? type = "annotation" ? expression = "org.springframework.stereotype.Controller" ? /> ??
- ?? < context:exclude-filter ? type = "annotation" ? expression = "org.springframework.stereotype.Service" ? /> ?
- < context:exclude-filter ? type = "annotation" ? expression = "org.springframework.stereotype.Repository" ? /> ? ?
- ?? </ context:component-scan > ??
Sping3.1和hibernate4.2集成—— No Session found for current thread
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
