我得到一個懶惰的初始化異常,我不明白...奇怪的懶惰初始化異常
我使用Java,Hibernate,Spring和Wicket。
因此,從Form的保存方法(擴展檢票窗體)時,我得到一個LazyInitializationException
當訪問一個對象的集合,但馬上我可以訪問另一個具有相同「配置」觸發該例外集合:
這裏是代碼的形式:
therapyGroup.getTherapies().clear();
therapyGroup.getTherapies().addAll(therapiesOldGroup);
therapyGroup.getToxicities().add(lastTherapy.getToxicity());
而且在這些地方集合類中定義的一部分:
@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;
}
「有問題」的收集是毒性收集。如果我先交換命令並調用毒性,它也會拋出LazyInitializationException
。這個例外總是由毒性而不是治療所解僱......爲什麼?
編輯:這裏是堆棧跟蹤
根源:
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)
是我叫therapyGroup.getToxicities().add(lastTherapy.getToxicity());
在同一行中有另一個呼叫:lastTherapy.getToxicity()。你確定這不是問題嗎? – kostja
如果沒有完整的堆棧跟蹤,包括所有「引起來」部分,則無法回答此問題。就在昨天,我看到了這個確切的例外,其中「Caused By」顯示數據庫連接失敗,即原因與代碼無關。這可能不是你的問題,但除非我們能看到整個堆棧跟蹤,否則答案可能是任何事情。 –
我已經添加了完整的堆棧跟蹤。 – diminuta