views:

114

answers:

2

I am building a project with Stripes, Spring, JPA & Hibernate amd have an object with a many to one child collection. I have set the loading as Lazy eg.

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

Now I am getting the LazyInitializationException when I get a collection of these objects from the database, even though at no stage am I directly accessing the child collection.

I thought lazy loading meant that it would only instantiate the child object collection when it was referenced? I have tried looking at similar issue but cannot figure this whole thing out so would appreciate any clarification.

The funny thing is that I have always had this relationship set up but only actually noticed the exception when I changed the database to have a proper foreign key relationship with the child table.

Edit: Stack Trace:

Stack Trace:

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)
A: 

Are you sure that you don't access the collection in the toString, hashCode or equals-methods? The later 2 could be used by hibernate to add the results to its cache.

Kurt Du Bois
No I don't - of course the child object (paperofferingassessment) uses the paperoffering in the hashcode and equals but would that cause a problem - still same issue about why is it instantiated at all
Mark
A: 

Lazy means that collection items will be loaded when someone will call get(index) or other method which needs to operate with fully initialized collection. size() doesn't initialize collection.

You error means, that your code, which tries to get element from collection, working outside from Hibernate session. Session was closed some time before so you get lazy detached collection.

Please, check that you code which operates with collection is executed within transaction. Maybe, yoiu'll need to mark your method with @Transactional annotation (if you using declarative description) or place you code within

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