日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Sping3.1和hibernate4.2集成—— No Session fo

系統(tǒng) 2694 0

在使用spring3和hibernate4.2集成與hibernate3有很多的不同,其中之一就是spring3不在支持HibernateTemplate,而是使用hibernate原生的api,我在集成的時候遇到了如下兩個問題。?


問題之一:在使用session.save()方法保存數(shù)據(jù)時不能成功的保存到數(shù)據(jù)庫 ?
??? 這個問題的原因是在獲取session時,不能使用openSession()方法,而要使用getCurrentSession()方法?

Java代碼?? 收藏代碼
  1. ??????
  2. @Resource (name= "sf" )??
  3. private ?SessionFactory?sessionFactory;???
  4. Session?session?;??
  5. ...??
  6. session?=?sessionFactory.getCurrentSession();??



問題之二:使用getCurrentSession時報:hibernate4 org.hibernate.HibernateException: No Session的錯誤 ?
??? 這個問題的原因是session未打開,解決方式:?
??? 前提記得在service層上面加上@Transaction注釋,否則任然會有相同的異常?

方式1-在web.xml中加過濾器?

Java代碼?? 收藏代碼
  1. <!--?open?session?filter?-->??
  2. <filter>??
  3. ????<filter-name>openSessionInViewFilter</filter-name>??
  4. ????<filter- class >org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter- class >??
  5. ????<init-param>??
  6. ????<param-name>singleSession</param-name>??
  7. ????<param-value> true </param-value>??
  8. ????</init-param>??
  9. </filter>??


方式2-配置事務(wù)的切面?

Java代碼?? 收藏代碼
  1. <!--?用spring管理事務(wù)?-->??
  2. ???<bean?id= "transactionManager" ? class = "org.springframework.orm.hibernate4.HibernateTransactionManager" >??????
  3. ???????<property?name= "sessionFactory" ?ref= "sf" />??????
  4. ???</bean>????
  5. ?????
  6. ???<!--?配置使用注解的方式來使用事務(wù)?-->???
  7. <tx:annotation-driven?transaction-manager= "transactionManager" ?/>??
  8. ??
  9. <!--?配置那些類的方法進(jìn)行事務(wù)管理,需要aopalliance- 1.0 .jar和aspectjweaver.jar,當(dāng)前com.neusoft.leehom.service包中的子包,????
  10. ??????????????????????類中所有方法需要,還需要參考tx:advice的設(shè)置?-->????
  11. ??
  12. ???<!--?這是事務(wù)通知操作,使用的事務(wù)管理器引用自?transactionManager?-->????
  13. ???<tx:advice?id= "txAdvice" ?transaction-manager= "transactionManager" >????
  14. ???????<tx:attributes>????
  15. ??
  16. ???????????<tx:method?name= "insert*" ?propagation= "REQUIRED" ?/>????
  17. ???????????<tx:method?name= "update*" ?propagation= "REQUIRED" ?/>????
  18. ???????????<tx:method?name= "delete*" ?propagation= "REQUIRED" ?/>????
  19. ???????????<tx:method?name= "get*" ?propagation= "REQUIRED" ?read-only= "true" />????
  20. ???????????<tx:method?name= "query*" ?propagation= "REQUIRED" ?read-only= "true" />????
  21. ???????????<tx:method?name= "*" ?propagation= "REQUIRED" ?/>????
  22. ???????</tx:attributes>????
  23. ???</tx:advice>???
  24. ?????
  25. ????<!--?需要引入aop的命名空間?-->????
  26. ???<aop:config>????
  27. ???????<!--?切入點指明了在執(zhí)行Service的所有方法時產(chǎn)生事務(wù)攔截操作?-->????
  28. ???????<aop:pointcut?id= "daoMethods" ?expression= "execution(*?com.tl..serviceimpl.*.*(..))" ?/>????????
  29. ???????<!--?定義了將采用何種攔截操作,這里引用到?txAdvice?-->????
  30. ???????<aop:advisor?advice-ref= "txAdvice" ?pointcut-ref= "daoMethods" ?/>????
  31. ???</aop:config> ??
?
?
?
?
?
-------------------分割線------------------------------------
?

在使用Spring mvc + Spring + Hibernate4 配置 的時候,總是出現(xiàn)?No Session found for current thread,仔細(xì)檢查applicationContext.xml和dispacter-servlet.xml文件,注解、事務(wù) 配置 都沒有 問題 ,糾結(jié)好久。

網(wǎng)上搜了很多方法,都不能 解決

有說加上 < prop ? key = "hibernate.current_session_context_class" > thread </ prop >的 配置 有說hibernate4要加上的是 < prop key = "hibernate.current_session_context_class" > org.springframework.orm.hibernate4.SpringSessionContext </ prop >,經(jīng)過測試,都不能 解決

看了http://blog.csdn.net/qq445422083/article/details/8160387帖子之后,覺得說的有道理,還是事務(wù)沒有裝配。

? ? ? ? 試著把 dispacter-servlet.xml中的內(nèi)容合并到 applicationContext.xml中,采用一個 配置 文件(當(dāng)然web.xml也要做相應(yīng)修改),發(fā)現(xiàn)并沒有報錯, 問題解決

? ? ? ? 但是自己還是喜歡使用兩個 配置 文件,這樣結(jié)構(gòu)更清晰。

? ? ? ? 分析為什么合并到一起就沒 問題 呢,原來 spring的context是父子容器,所以會產(chǎn)生沖突,Controller會首先被掃描裝配,而此時的Service還沒有進(jìn)行事務(wù)的 配置 ,獲得的將是原樣的Service(沒有經(jīng)過事務(wù)裝配,故而沒有事務(wù)處理能力) ,最后才是applicationContext.xml中的掃描設(shè)備進(jìn)行事務(wù)處理。

這樣就好辦了,讓兩個 配置 文件各干各的事就可以了。

1 、在Spring主容器中(applicationContext.xml),用 < context:exclude-filter/> 將Controller的注解過濾掉,不掃描裝配它。

?

[html] ?view plaincopy
?
  1. < context:component-scan ? base-package = "com" > ??
  2. ?? < context:exclude-filter ? type = "annotation" ? expression = "org.springframework.stereotype.Controller" ? /> ??
  3. </ context:component-scan > ???

2 而在springMVC 配置 文件中(dispatcher-servlet.xml)將Service和Dao的注解給 過濾 掉 ,只掃描裝配 Controller。

?

?

[html] ?view plaincopy
?
  1. < context:component-scan ? base-package = "com" > ??
  2. ?? < context:include-filter ? type = "annotation" ? expression = "org.springframework.stereotype.Controller" ? /> ??
  3. ?? < context:exclude-filter ? type = "annotation" ? expression = "org.springframework.stereotype.Service" ? /> ?
  4. < context:exclude-filter ? type = "annotation" ? expression = "org.springframework.stereotype.Repository" ? /> ? ?
  5. ?? </ context:component-scan > ??

Sping3.1和hibernate4.2集成—— No Session found for current thread


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 勃利县| 高州市| 汾阳市| 马龙县| 肇庆市| 韩城市| 蒙自县| 乃东县| 广安市| 平安县| 宣汉县| 旌德县| 湾仔区| 西畴县| 资溪县| 嘉荫县| 肇庆市| 安西县| 磴口县| 宜丰县| 丁青县| 安新县| 绥中县| 云龙县| 疏附县| 巴彦县| 临朐县| 蕉岭县| 神农架林区| 内江市| 广河县| 龙南县| 鄂州市| 樟树市| 大洼县| 府谷县| 深水埗区| 紫阳县| 永安市| 农安县| 忻城县|