I am getting a lazy initialization exception that I don't understand...
I work with Java, Hibernate, Spring and Wicket.
So, from the save method of the Form (extends wicket Form) I get a LazyInitializationException
when accesing a colletion of an object, but right away I can access another collection of the same object which has the same "configuration" as the collection that fires the exception:
Here is the code in the Form:
therapyGroup.getTherapies().clear();
therapyGroup.getTherapies().addAll(therapiesOldGroup);
therapyGroup.getToxicities().add(lastTherapy.getToxicity());
And here the part where the collections are defined in the class:
@OneToMany(mappedBy = "therapyGroup", fetch = FetchType.LAZY, orphanRemoval=true)
@Cascade(value = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.DELETE, CascadeType.SAVE_UPDATE })
@OrderBy(value = "date asc")
@Filters( { @Filter(name = "deletedFilter", condition = "deleted <> :deletedParam") })
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL, region = "TherapyGroup")
@Lazy
public Set<Therapy> getTherapies() {
return therapies;
}
@OneToMany(mappedBy = "therapyGroup", fetch = FetchType.LAZY, orphanRemoval=true)
@Cascade(value = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.DELETE, CascadeType.SAVE_UPDATE })
@OrderBy(value = "date asc")
@Filters( { @Filter(name = "deletedFilter", condition = "deleted <> :deletedParam") })
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL, region = "TherapyGroup")
@Lazy
public Set<Toxicity> getToxicities() {
return toxicities;
}
The "Problematic" collection is the toxicities collection. If I swap the order and call toxicities first, it also throws the LazyInitializationException
. The exception is always fired by the toxicities and not by the therapies... why?
EDIT: Here's the stack trace
Root cause:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.mycompany.myapp.data.TherapyGroup.toxicities, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368)
at org.hibernate.collection.PersistentSet.add(PersistentSet.java:212)
at com.mycompany.myapp.web.support.therapyend.TherapyEndSupportForm.onSaveFormData(TherapyEndSupportForm.java:135)
at com.mycompany.myapp.web.base.BaseForm.doSave(BaseForm.java:370)
at com.mycompany.myapp.web.base.BaseForm.saveAndTrigger(BaseForm.java:1137)
at com.mycompany.myapp.web.base.BaseForm.switchModalWindow(BaseForm.java:1128)
at com.mycompany.myapp.web.base.BaseForm.switchModalWindow(BaseForm.java:1077)
at com.mycompany.myapp.web.base.BaseForm.onSubmit(BaseForm.java:567)
at com.mycompany.myapp.web.comp.QuasiAjaxSubmitButton.onSubmit(QuasiAjaxSubmitButton.java:49)
at com.mycompany.myapp.web.comp.QuasiAjaxButton$1.onSubmit(QuasiAjaxButton.java:65)
at com.mycompany.myapp.web.comp.QuasiAjaxFormSubmitBehavior.onEvent(QuasiAjaxFormSubmitBehavior.java:151)
at org.apache.wicket.ajax.AjaxEventBehavior.respond(AjaxEventBehavior.java:177)
at org.apache.wicket.ajax.AbstractDefaultAjaxBehavior.onRequest(AbstractDefaultAjaxBehavior.java:286)
at org.apache.wicket.request.target.component.listener.BehaviorRequestTarget.processEvents(BehaviorRequestTarget.java:119)
at org.apache.wicket.request.AbstractRequestCycleProcessor.processEvents(AbstractRequestCycleProcessor.java:92)
at org.apache.wicket.RequestCycle.processEventsAndRespond(RequestCycle.java:1250)
at org.apache.wicket.RequestCycle.step(RequestCycle.java:1329)
at org.apache.wicket.RequestCycle.steps(RequestCycle.java:1428)
at org.apache.wicket.RequestCycle.request(RequestCycle.java:545)
at org.apache.wicket.protocol.http.WicketFilter.doGet(WicketFilter.java:479)
at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:312)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1139)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1139)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:378)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:417)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:535)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:880)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:747)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.jetty.bio.SocketConnector$Connection.run(SocketConnector.java:228)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:520)
com.mycompany.myapp.web.support.therapyend.TherapyEndSupportForm.onSaveFormData(TherapyEndSupportForm.java:135)
is the line where I call therapyGroup.getToxicities().add(lastTherapy.getToxicity());