2010-08-06 85 views
0

我正在建設一個帶Stripes,Spring,JPA &的項目Hibernate amd有一個多對一的子集合的對象。例如,我已將加載設置爲「惰性」 。JPA - 懶加載 - LazyInitializationException - 當不訪問子集合

@OneToMany(cascade = CascadeType.MERGE, mappedBy = "paperOffering", fetch = FetchType.LAZY) 
private List<PaperOfferingAssessment> paperOfferingAssessments; 

現在我正在LazyInitializationException中,當我得到這些對象的集合,從數據庫中,即使在任何階段我會直接訪問子集。

我以爲延遲加載意味着它只會在被引用時實例化子對象集合? 我曾試着尋找類似的問題,但不能把這件事整理出來,所以將不勝感激任何澄清。

有趣的是,我一直建立這種關係,但實際上只是在我將數據庫更改爲與子表具有適當的外鍵關係時才注意到異常。

編輯:堆棧跟蹤:

堆棧跟蹤:

17:39:17] ERROR org.hibernate.LazyInitializationException - failed to lazily initialize a collection of role: eis.model.PaperOffering.paperOfferingAssessments, no session or session was closed 
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: eis.model.PaperOffering.paperOfferingAssessments, no session or session was closed 
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380) 
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372) 
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365) 
    at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108) 
    at org.hibernate.collection.PersistentSet.toArray(PersistentSet.java:194) 
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildCollectionNode(JavaScriptBuilder.java:524) 
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildNode(JavaScriptBuilder.java:365) 
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildObjectNode(JavaScriptBuilder.java:420) 
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildNode(JavaScriptBuilder.java:374) 
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildArrayNode(JavaScriptBuilder.java:507) 
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildCollectionNode(JavaScriptBuilder.java:524) 
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildNode(JavaScriptBuilder.java:365) 
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.build(JavaScriptBuilder.java:191) 
    at net.sourceforge.stripes.ajax.JavaScriptResolution.execute(JavaScriptResolution.java:75) 
    at net.sourceforge.stripes.controller.DispatcherHelper$7.intercept(DispatcherHelper.java:508) 
    at net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:158) 
    at net.sourceforge.stripes.controller.HttpCacheInterceptor.intercept(HttpCacheInterceptor.java:99) 
    at net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:155) 
    at net.sourceforge.stripes.controller.BeforeAfterMethodInterceptor.intercept(BeforeAfterMethodInterceptor.java:113) 
    at net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:155) 
    at net.sourceforge.stripes.controller.ExecutionContext.wrap(ExecutionContext.java:74) 
    at net.sourceforge.stripes.controller.DispatcherHelper.executeResolution(DispatcherHelper.java:502) 
    at net.sourceforge.stripes.controller.DispatcherServlet.executeResolution(DispatcherServlet.java:286) 
    at net.sourceforge.stripes.controller.DispatcherServlet.service(DispatcherServlet.java:170) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) 
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:527) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1216) 
    at net.sourceforge.stripes.controller.StripesFilter.doFilter(StripesFilter.java:247) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1187) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:343) 
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109) 
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) 
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) 
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) 
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) 
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) 
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) 
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:188) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) 
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) 
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) 
    at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:109) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) 
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:149) 
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237) 
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1187) 
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:421) 
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119) 
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:493) 
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:225) 
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:930) 
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:358) 
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:183) 
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:866) 
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117) 
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:245) 
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:126) 
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:113) 
    at org.eclipse.jetty.server.Server.handle(Server.java:351) 
    at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:594) 
    at org.eclipse.jetty.server.HttpConnection$RequestHandler.content(HttpConnection.java:1059) 
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:764) 
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:217) 
    at org.eclipse.jetty.server.HttpConnection.handle(HttpConnection.java:424) 
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:506) 
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:436) 
    at java.lang.Thread.run(Thread.java:619) 
[17:39:17] WARN net.sourceforge.stripes.ajax.JavaScriptBuilder - Could not translate property [paperOfferingAssessments] of type [java.util.Set] due to an exception. 
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: eis.model.PaperOffering.paperOfferingAssessments, no session or session was closed 
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380) 
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372) 
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365) 
    at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108) 
    at org.hibernate.collection.PersistentSet.toArray(PersistentSet.java:194) 
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildCollectionNode(JavaScriptBuilder.java:524) 
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildNode(JavaScriptBuilder.java:365) 
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildObjectNode(JavaScriptBuilder.java:420) 
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildNode(JavaScriptBuilder.java:374) 
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildArrayNode(JavaScriptBuilder.java:507) 
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildCollectionNode(JavaScriptBuilder.java:524) 
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildNode(JavaScriptBuilder.java:365) 
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.build(JavaScriptBuilder.java:191) 
    at net.sourceforge.stripes.ajax.JavaScriptResolution.execute(JavaScriptResolution.java:75) 
    at net.sourceforge.stripes.controller.DispatcherHelper$7.intercept(DispatcherHelper.java:508) 
    at net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:158) 
    at net.sourceforge.stripes.controller.HttpCacheInterceptor.intercept(HttpCacheInterceptor.java:99) 
    at net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:155) 
    at net.sourceforge.stripes.controller.BeforeAfterMethodInterceptor.intercept(BeforeAfterMethodInterceptor.java:113) 
    at net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:155) 
    at net.sourceforge.stripes.controller.ExecutionContext.wrap(ExecutionContext.java:74) 
    at net.sourceforge.stripes.controller.DispatcherHelper.executeResolution(DispatcherHelper.java:502) 
    at net.sourceforge.stripes.controller.DispatcherServlet.executeResolution(DispatcherServlet.java:286) 
    at net.sourceforge.stripes.controller.DispatcherServlet.service(DispatcherServlet.java:170) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) 
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:527) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1216) 
    at net.sourceforge.stripes.controller.StripesFilter.doFilter(StripesFilter.java:247) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1187) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:343) 
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109) 
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) 
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) 
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) 
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) 
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) 
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) 
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:188) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) 
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) 
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) 
    at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:109) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355) 
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:149) 
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237) 
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1187) 
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:421) 
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119) 
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:493) 
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:225) 
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:930) 
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:358) 
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:183) 
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:866) 
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117) 
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:245) 
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:126) 
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:113) 
    at org.eclipse.jetty.server.Server.handle(Server.java:351) 
    at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:594) 
    at org.eclipse.jetty.server.HttpConnection$RequestHandler.content(HttpConnection.java:1059) 
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:764) 
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:217) 
    at org.eclipse.jetty.server.HttpConnection.handle(HttpConnection.java:424) 
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:506) 
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:436) 
    at java.lang.Thread.run(Thread.java:619) 
+0

@mrtrombone後堆棧跟蹤,以及如何將你的代碼你找回你的實體 – 2010-08-06 20:59:26

+0

實體與基本檢索命名查詢 - 「選擇p from paperoffering p where year =:year」 在這個特定的實例中,我只使用name和id字段來填充選擇列表,所以我應該創建一個簡單的DTO,但我只想首先理解 – Mark 2010-08-07 08:40:41

回答

0

你確定你不訪問集合中的toString,或的hashCode等於的方法?後面的2可以被hibernate用來將結果添加到它的緩存中。

+0

不,我不喜歡當然,子對象(paperofferingassessment)使用哈希碼中的paperoffering並且等於但會導致問題 - 仍然是爲什麼它實例化的問題 – Mark 2010-08-07 08:43:39

1

懶惰表示當有人打電話get(index)或其他需要完全初始化收集操作的方法時,收集項目將被加載。 size()不會初始化集合。

你的錯誤意味着,你的代碼試圖從集合中獲取元素,在Hibernate會話之外工作。會議前一段時間關閉,所以你得到懶惰的分離收集。

請檢查您的代碼是否與收集一起在交易中執行。也許,yoiu'll需要@Transactional標註來標記你的方法(如果你使用聲明描述),或在

TransactionManager tm = //get TransactionManager 
try { 
    tm.begin(); 
    //your code 
    tm.commit(); 
} catch (Exception e) { 
    tm.rollback(); 
}