奇怪的Lazy初始化异常

时间:2014-06-18 15:37:03

标签: java spring hibernate wicket lazy-initialization

我得到一个我不理解的延迟初始化异常......

我使用Java,Hibernate,Spring和Wicket。

因此,从Form的save方法(扩展wicket表单)中,我在访问一个对象的集合时得到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());

的行

2 个答案:

答案 0 :(得分:1)

在两种方法中设置断点。当您运行代码时,您会在某处看到getTherapies()在事务内部被调用。这意味着你有一个集合,当你在save()内部调用方法时,Hibernate将使用它而不是试图从数据库加载它。

getToxicities()的情况并非如此。因此Hibernate尝试加载它但没有当前事务,因此加载失败。因此,请确保save()从某处获得交易。也许你忘记了某处的注释?

答案 1 :(得分:0)

当我忘记在服务类中编写@Transaction注释时,我会用这种异常。