2014-06-18 187 views
-2

我得到一個懶惰的初始化異常,我不明白...奇怪的懶惰初始化異常

我使用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());

+1

在同一行中有另一個呼叫:lastTherapy.getToxicity()。你確定這不是問題嗎? – kostja

+1

如果沒有完整的堆棧跟蹤,包括所有「引起來」部分,則無法回答此問題。就在昨天,我看到了這個確切的例外,其中「Caused By」顯示數據庫連接失敗,即原因與代碼無關。這可能不是你的問題,但除非我們能看到整個堆棧跟蹤,否則答案可能是任何事情。 –

+0

我已經添加了完整的堆棧跟蹤。 – diminuta

回答

0

我用它來獲得這種例外的時候我忘了寫的行@Transaction註釋在我的服務類。

1

在兩種方法中設置斷點。當你運行代碼時,你會發現在某個地方,getTherapies()在一個事務中被調用。這意味着你有一個集合,當你調用save()中的方法時,Hibernate將使用它而不是從數據庫中加載它。

對於getToxicities()也是如此。所以Hibernate試圖加載它,但沒有當前事務,所以加載失敗。所以請確保save()從某處獲取事務。也許你忘了某個地方的註釋?

相關問題