gpt4 book ai didi

java - wicket/hibernate 的 LazyInitialization 异常

转载 作者:行者123 更新时间:2023-12-01 19:09:25 25 4
gpt4 key购买 nike

我在一个简单的网络应用程序上使用 wicket 和 hibernate (jpa)。
Task.java与Load.java的关系如下:

任务.java:

@ManyToOne( targetEntity=Load.class, optional=true, fetch=FetchType.LAZY)   
@JoinColumn(name="load_id")
public Load getLoad() {
return load;
}

一个任务最多可以有一个负载,但一个负载可以映射到多个任务:
加载.java:

@OneToMany( mappedBy="load",targetEntity=Task.class,  orphanRemoval=false)
public Set<Task> getTasks() {
return tasks;
}

正在创建 session (JPA 中的实体管理器)并通过事务过滤器附加到线程。

我有一个列出任务的页面,并显示前 10 个任务。当我单击下一个 10 个任务的链接时,它会尝试加载任务 11-20 的负载(使用 getLoad),并且由于某种原因抛出LazyInitializationException - 即使该线程有一个有效的 session (如堆栈跟踪和 debuggin 中所示)。我不能使用急切加载,因为这会在其他地方引起问题。我可以检查实体管理器在 LinkPanel 的构造函数中是否有效,并且它确实有效。然而 3 行之后,它抛出了 LazyInitializationException。这是怎么回事?

- could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
at com.xxx.er.batch.beans.Load_$$_javassist_7.toString(Load_$$_javassist_7.java)
at com.xxx.er.basman.model.LinkPanel.<init>(LinkPanel.java:41)
at com.xxx.er.basman.pages.TasksOverviewPage$7.populateItem(TasksOverviewPage.java:130)
at org.apache.wicket.extensions.markup.html.repeater.data.grid.AbstractDataGridView.populateItem(AbstractDataGridView.java:187)
at org.apache.wicket.markup.repeater.RefreshingView$1.newItem(RefreshingView.java:114)
at org.apache.wicket.markup.repeater.DefaultItemReuseStrategy$1.next(DefaultItemReuseStrategy.java:71)
at org.apache.wicket.markup.repeater.DefaultItemReuseStrategy$1.next(DefaultItemReuseStrategy.java:68)
at org.apache.wicket.markup.repeater.RefreshingView.addItems(RefreshingView.java:189)
at org.apache.wicket.markup.repeater.RefreshingView.onPopulate(RefreshingView.java:98)
at org.apache.wicket.markup.repeater.AbstractRepeater.onBeforeRender(AbstractRepeater.java:131)
at org.apache.wicket.markup.repeater.AbstractPageableView.onBeforeRender(AbstractPageableView.java:121)
at org.apache.wicket.Component.internalBeforeRender(Component.java:1066)
at org.apache.wicket.Component.beforeRender(Component.java:1100)
at org.apache.wicket.MarkupContainer.onBeforeRenderChildren(MarkupContainer.java:1754)
at org.apache.wicket.Component.onBeforeRender(Component.java:3966)
at org.apache.wicket.Component.internalBeforeRender(Component.java:1066)
at org.apache.wicket.Component.beforeRender(Component.java:1100)
at org.apache.wicket.MarkupContainer.onBeforeRenderChildren(MarkupContainer.java:1754)
at org.apache.wicket.Component.onBeforeRender(Component.java:3966)
at org.apache.wicket.Component.internalBeforeRender(Component.java:1066)
at org.apache.wicket.Component.beforeRender(Component.java:1100)
at org.apache.wicket.MarkupContainer.onBeforeRenderChildren(MarkupContainer.java:1754)
at org.apache.wicket.Component.onBeforeRender(Component.java:3966)
at org.apache.wicket.Page.onBeforeRender(Page.java:1550)
at org.apache.wicket.Component.internalBeforeRender(Component.java:1066)
at org.apache.wicket.Component.beforeRender(Component.java:1100)
at org.apache.wicket.Component.prepareForRender(Component.java:2292)
at org.apache.wicket.Page.prepareForRender(Page.java:1540)
at org.apache.wicket.Component.prepareForRender(Component.java:2329)
at org.apache.wicket.Page.renderPage(Page.java:911)
at org.apache.wicket.protocol.http.WebRequestCycle.redirectTo(WebRequestCycle.java:201)
at org.apache.wicket.request.target.component.PageRequestTarget.respond(PageRequestTarget.java:58)
at org.apache.wicket.request.AbstractRequestCycleProcessor.respond(AbstractRequestCycleProcessor.java:105)
at org.apache.wicket.RequestCycle.processEventsAndRespond(RequestCycle.java:1258)
at org.apache.wicket.RequestCycle.step(RequestCycle.java:1329)
at org.apache.wicket.RequestCycle.steps(RequestCycle.java:1436)
at org.apache.wicket.RequestCycle.request(RequestCycle.java:545)
at org.apache.wicket.protocol.http.WicketFilter.doGet(WicketFilter.java:486)
at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:319)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1088)
at com.xxx.er.basman.HibernateTransactionFilter.doFilter(HibernateTransactionFilter.java:58)

重申一下,HibernateTransactionFilter 创建一个如下所示的 EntityManager:
EntityManager em = myEntityManagerFactory.createEntityManager();
em.getTransaction().begin();
entityManagerThreadLocal.set(em);

在 LinkPanel 构造函数中,我按如下方式检查此 EntityManager:

EntityManager em = entityManagerThreadLocal.get();
if (em == null) {
throw new NullPointerException("No entity manager has been started on this thread: " + Thread.currentThread().getName());
}

最佳答案

仅仅因为实体管理器打开并不意味着可以加载所有延迟加载的实体。如果实体是由与“当前”实体管理器/ session 不同的实体管理器/ session 加载的,则实体仍然是分离的,并且尝试在它们上加载惰性关联将导致您遇到的异常。

简而言之,您的过滤器会打​​开一个实体管理器,这使得当前请求可以进行延迟加载。但一旦请求结束,实体就会分离。

在第二个请求时重新加载它们,或者将它们重新附加到当前实体管理器/ session 。

关于java - wicket/hibernate 的 LazyInitialization 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8787857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com