亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

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

系統 2277 0

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


問題之一:在使用session.save()方法保存數據時不能成功的保存到數據庫 ?
??? 這個問題的原因是在獲取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-配置事務的切面?

Java代碼?? 收藏代碼
  1. <!--?用spring管理事務?-->??
  2. ???<bean?id= "transactionManager" ? class = "org.springframework.orm.hibernate4.HibernateTransactionManager" >??????
  3. ???????<property?name= "sessionFactory" ?ref= "sf" />??????
  4. ???</bean>????
  5. ?????
  6. ???<!--?配置使用注解的方式來使用事務?-->???
  7. <tx:annotation-driven?transaction-manager= "transactionManager" ?/>??
  8. ??
  9. <!--?配置那些類的方法進行事務管理,需要aopalliance- 1.0 .jar和aspectjweaver.jar,當前com.neusoft.leehom.service包中的子包,????
  10. ??????????????????????類中所有方法需要,還需要參考tx:advice的設置?-->????
  11. ??
  12. ???<!--?這是事務通知操作,使用的事務管理器引用自?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. ???????<!--?切入點指明了在執行Service的所有方法時產生事務攔截操作?-->????
  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 配置 的時候,總是出現?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的注解過濾掉,不掃描裝配它。

?

[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


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产一区二区久久久 | 青草福利| 96精品国产高清在线看入口 | 国产成人高清亚洲一区91 | 在线观看亚洲免费视频 | 99久久99久久精品免费看子 | 国产一区二区三区欧美 | 一级片看看 | 久草视频在线资源 | 97伊人久久| 久久的爱久久久久的快乐 | 久久久久嫩草影院精品 | 日韩欧美高清视频 | 99爱视频免费高清在线观看 | 特级a欧美做爰片毛片 | 亚洲精品乱码国产精品乱码 | 国产午夜永久福利视频在线观看 | 国产女人18一级毛片视频 | 国产香蕉一区二区精品视频 | 亚洲另类图 | 色噜噜狠狠狠狠色综合久一 | 亚洲乱码一区二区三区国产精品 | 日韩欧美视频一区 | 色综合影视| a毛片在线还看免费网站 | 日韩一级欧美一级一级国产 | 在线观看人成网站深夜免费 | 高清国产美女一级a毛片录 高清国产美女一级毛片 | xxxxyoujizz护士| 久久一区二区免费播放 | 日韩第3页 | 中文字幕亚洲欧美日韩高清 | 久久综合亚洲一区二区三区 | 91中文字幕在线视频 | 2021久久伊人精品中文字幕有 | 日日综合网 | 2021最新国产成人精品视频 | 国产色综合久久无码有码 | 亚洲人成一区 | 日日夜夜嗷嗷叫 | 久久视频一区 |