2010-10-11 25 views
0

我使用Spring MVC框架與Hibernate。我所有的控制器都使用OpenSessionInViewInterceptor。我如何重新打開一個Spring會話與OpenSessionInViewInterceptor

我在控制器的onSubmit()方法中收到「無法初始化代理 - 擁有會話已關閉」錯誤。我相信這是因爲Hibernate需要返回數據庫來獲取某些作爲代理檢索的對象,並且OpenSessionInViewInterceptor已經關閉了會話。

我一直在解決這些問題,在對象上使用HibernateTemplate()。reattach()。

我的問題是:

a)我的診斷正確嗎? 和b)是否有更好的方法來重新打開Hibernate會話?有沒有辦法以編程方式重新打開會議在Hibernate沒有reattach()?

編輯:堆棧跟蹤低於:當我遍歷集合在我提交方法我得到的錯誤

Data access failure: failed to lazily initialize a collection of role: com.companyname.apps.manager.domain.PriceChangeset.priceChanges, no session or session was closed 

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.companyname.apps.manager.domain.PriceChangeset.priceChanges, no session or session was closed 
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358) 
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350) 
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343) 
    at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86) 
    at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:163) 
    **at com.companyname.apps.manager.service.impl.PriceChangesetServiceImpl.addChangesToSqlTransfer(PriceChangesetServiceImpl.java:263) 
    at com.companyname.apps.manager.service.impl.PriceChangesetServiceImpl.approve(PriceChangesetServiceImpl.java:185)** 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:585) 
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:299) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:139) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) 
    at $Proxy100.approve(Unknown Source) 
    at com.companyname.apps.manager.webapp.controller.pricechanges.PriceChangeDetailsController.onSubmit(PriceChangeDetailsController.java:115) 

謝謝!

+1

「OpenSessionInViewInterceptor」的重點在於你不必*重新打開會話。異常在哪裏發生? – skaffman 2010-10-11 18:16:14

+0

它在我的onSubmit()中,我剛剛發佈了棧跟蹤 – benhsu 2010-10-11 19:24:38

+0

您是否試圖讓一個會話跨多個請求打開? – skaffman 2010-10-11 19:33:07

回答

2

OpenSessionInViewInterceptor旨在讓會話在單個請求期間保持打開狀態。它不是爲了讓會話在多個請求中打開而設計的。事實上,做這樣的事情是強烈的不鼓勵。一旦請求完成,會話應該被釋放。

如果你想一個Hibernate對象重新連接到一個新的會話,作爲一個新的請求的一部分,那麼你需要使用類似reattach()merge()等,在休眠SessionEntitymManagerOpenSessionInViewInterceptor不會幫你在這裏。

相關問題