1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      解決hibernate報錯:no-session的問題-創(chuàng)新互聯(lián)

      1.問題描述:對于根據(jù)id查詢時,在dao通過load方式查詢對象時,加載頁面會報 noSession異常。

      義縣網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。成都創(chuàng)新互聯(lián)成立與2013年到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)

      解決hibernate報錯:no-session的問題

       嚴(yán)重: Servlet.service() for servlet [springDispatcherServlet] in context with path [/Xxxx] threw exception 
      
      [Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread] 
      
      with root cause
      org.hibernate.HibernateException: No Session found for current thread
          at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)
          at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988)
          at com.vincent.videosys.dao.BaseDao.getSession(BaseDao.java:17)
          at com.vincent.videosys.dao.UserDao.usernameExist(UserDao.java:29)
          at com.vincent.videosys.service.UserService.usernameExistService(UserService.java:19)
          at com.vincent.videosys.controller.home.UserController.usernameExist(UserController.java:40)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
          at java.lang.reflect.Method.invoke(Unknown Source)
          at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
          at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
          at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle

      解決hibernate報錯:no-session的問題

      2.問題分析:當(dāng)使用hibernate框架操作數(shù)據(jù)庫的時候,如果做查詢的話會有立即加載(get)和延遲加載(load)的區(qū)別,延遲加載表示,當(dāng)你查詢某個數(shù)據(jù)(假設(shè)是對象)的時候,hibernate不會立馬發(fā)送sql語句,而是當(dāng)我們調(diào)用這個對象的屬性的時候,也就是真正使用查詢出來的數(shù)據(jù)的時候才會發(fā)送sql語句去一級緩存(即session,這里的session和域?qū)ο髎ession沒有半毛錢關(guān)系)中獲取,但是正常這個session的開啟核關(guān)閉是在service層執(zhí)行的,但是我們真正使用查詢的對象的數(shù)據(jù)時,是在web層,但是這個時候session已經(jīng)關(guān)閉,就會報no-session異常。

      解決hibernate報錯:no-session的問題

      noSession分析圖(右鍵"查看圖像"查看原圖)

      3.問題解決思路

      方案一:讓session的關(guān)閉時間要在web層使用完之后。 但是web層已經(jīng)是最后一層了,怎么辦?還有比web更后的東西哦,就是過濾器, 所以在web.xml中配置開啟和關(guān)閉session的過濾器即可 ,但是要配在struts的過濾器之前,否則無效。

      解決hibernate報錯:no-session的問題

      
          OpenSessionInViewFilter
          org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
      
      
          OpenSessionInViewFilter
          *.action
      

      解決hibernate報錯:no-session的問題

      解決hibernate報錯:no-session的問題

      添加過濾器解決noSession分析圖

      使用load方法的解決方案 : 就是把原CustomerService層的綁定的session對象 提取配置到前面的 過濾器中了。

      方案二:

          load改用get立即加載方式查詢對象。

      解決hibernate報錯:no-session的問題

      package cn.xdf.dao.impl;
      @Repositorypublic class CustomerDaoImpl implements CustomerDao {
      
          @Autowired    
          rivate HibernateTemplate hibernateTemplate;    
          public void save(Customer customer) {
              hibernateTemplate.save(customer);}   
          public List findAll() {        
                 return (List) hibernateTemplate.find("from Customer"); }   
          public void delete(Customer customer) {
              hibernateTemplate.delete(customer);}    //根據(jù)id立即加載
            public Customer get(Long custId) {        
                  return hibernateTemplate.get(Customer.class, custId);}    //根據(jù)id延遲加載-->用該方法會有問題(頁面報錯:noSession)
            public Customer load(Long custId) {  
              return hibernateTemplate.load(Customer.class, custId);}  
            public void update(Customer customer) {
              hibernateTemplate.update(customer);}    
          public List findByCriteria(DetachedCriteria dc) {       
               return (List) hibernateTemplate.findByCriteria(dc);}
      }

      另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。


      本文名稱:解決hibernate報錯:no-session的問題-創(chuàng)新互聯(lián)
      文章出自:http://www.ef60e0e.cn/article/deeojo.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        阿拉善右旗| 固镇县| 湖南省| 巍山| 恭城| 福泉市| 湄潭县| 合山市| 岳池县| 凤冈县| 富阳市| 山东| 山西省| 昭觉县| 伊吾县| 连平县| 子长县| 慈溪市| 岢岚县| 安国市| 云安县| 延庆县| 都江堰市| 贵港市| 德清县| 泸西县| 岳阳县| 十堰市| 永安市| 定陶县| 南开区| 清丰县| 海安县| 十堰市| 汝城县| 邹城市| 南开区| 筠连县| 谷城县| 曲松县| 兴化市|