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


嚴(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

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異常。

noSession分析圖(右鍵"查看圖像"查看原圖)
3.問題解決思路
方案一:讓session的關(guān)閉時間要在web層使用完之后。 但是web層已經(jīng)是最后一層了,怎么辦?還有比web更后的東西哦,就是過濾器, 所以在web.xml中配置開啟和關(guān)閉session的過濾器即可 ,但是要配在struts的過濾器之前,否則無效。

<filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping>


添加過濾器解決noSession分析圖
使用load方法的解決方案 : 就是把原CustomerService層的綁定的session對象 提取配置到前面的 過濾器中了。
方案二:
load改用get立即加載方式查詢對象。

package cn.xdf.dao.impl;
@Repositorypublic class CustomerDaoImpl implements CustomerDao {
@Autowired
rivate HibernateTemplate hibernateTemplate;
public void save(Customer customer) {
hibernateTemplate.save(customer);}
public List<Customer> findAll() {
return (List<Customer>) 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<Customer> findByCriteria(DetachedCriteria dc) {
return (List<Customer>) 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)用場景需求。
網(wǎng)頁題目:解決hibernate報錯:no-session的問題-創(chuàng)新互聯(lián)
本文地址:http://www.chinadenli.net/article20/deeojo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、網(wǎng)站內(nèi)鏈、軟件開發(fā)、網(wǎng)站排名、企業(yè)建站、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容