gpt4 book ai didi

java - 访问@Id 时出现 LazyInitializationException

转载 作者:行者123 更新时间:2023-11-30 11:35:25 24 4
gpt4 key购买 nike

我们的 wicket/spring/hibernate 应用程序中出现 org.hibernate.LazyInitializationException。这种情况很少发生,可能每 100 次代码运行一次。

起初,我们在访问延迟加载的对象数据时遇到了异常(也是大约每 100 次代码运行一次)。所以我们认为如果我们通过从数据库中获取它的 id 来手动将对象加载到内存中,这个问题就会得到解决。我们的理解是,用javax.persistence.Id 注释的字段应该一直在内存中。令我们惊讶的是,我们仍然在访问 id 时 遇到异常。也许一个例子会更清楚..

这些是我们的 POJO:

public abstract class SuperDimension{ 

@Id
@GeneratedValue(generator = "Dimension_id_gen", strategy = GenerationType.TABLE)
@GenericGenerator(name = "Dimension_id_gen", strategy = "uuid")
protected String UUID;

/*getters, setters and abstract stuff*/

}

public class Dimension extends SuperDimension{

private String number;

/*getters, setters and overridden methods*/
}

public class DimensionHolder{

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "myDim")
private Dimension myDim;

/*getters, setters*/
}

我们有一个 DimensionHolder 的实例,想要获取它的 Dimension (myDims) 编号。起初我们这样做:

dimensionHolder.getMyDim().getNumber();

这导致(每第 100 次).getNumber() 上的延迟初始化异常。这是我们在访问号码之前尝试手动加载它的时候:

Dimension freshDimensionFromDataBase = dataBase.getDimensionFromUUID(dimensionHolder.getMyDim().getUUID);
String number = freshDimensionFromDataBase.getNumber();

这仍然会导致 .getUUID() 上的惰性初始化异常。即使 myDim 是代理,id 不应该仍然可用吗?

问题是否可能是 id 在 Dimensions 父类(super class)中,而我们正在使用的 Dimension 当前实例是来自 hibernate 的代理对象?这在我们听来也很奇怪,所以也许有好心人可以为我们的问题提供一些启示?

谢谢!

编辑:这里是堆栈跟踪:

Root cause: org.hibernate.LazyInitializationException: could not initialize proxy - no Session Root Cause Message: LazyInitializationException: could not initialize proxy - no Session
Message: WicketRuntimeException: Method onRequest of interface org.apache.wicket.behavior.IBehaviorListener targeted at eyesys.web.invoice.invoice.InvoicePage$20 {event='onclick'} on component [WebMarkupContainer [Component id = okayButton]] threw an exception
at org.apache.wicket.RequestListenerInterface.internalInvoke(RequestListenerInterface.java:270)
at org.apache.wicket.RequestListenerInterface.invoke(RequestListenerInterface.java:241)
at org.apache.wicket.request.handler.ListenerInterfaceRequestHandler.invokeListener(ListenerInterfaceRequestHandler.java:255)
at org.apache.wicket.request.handler.ListenerInterfaceRequestHandler.respond(ListenerInterfaceRequestHandler.java:234)
at org.apache.wicket.request.cycle.RequestCycle$HandlerExecutor.respond(RequestCycle.java:781)
at org.apache.wicket.request.RequestHandlerStack.execute(RequestHandlerStack.java:64)
at org.apache.wicket.request.cycle.RequestCycle.execute(RequestCycle.java:255)
at org.apache.wicket.request.cycle.RequestCycle.processRequest(RequestCycle.java:212)
at org.apache.wicket.request.cycle.RequestCycle.processRequestAndDetach(RequestCycle.java:283)
at org.apache.wicket.protocol.http.WicketFilter.processRequest(WicketFilter.java:185)
at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:241)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:600)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1703)
at java.lang.Thread.run(Thread.java:662)
Caused by:

at sun.reflect.GeneratedMethodAccessor78.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.wicket.RequestListenerInterface.internalInvoke(RequestListenerInterface.java:260)
at org.apache.wicket.RequestListenerInterface.invoke(RequestListenerInterface.java:241)
at org.apache.wicket.request.handler.ListenerInterfaceRequestHandler.invokeListener(ListenerInterfaceRequestHandler.java:255)
at org.apache.wicket.request.handler.ListenerInterfaceRequestHandler.respond(ListenerInterfaceRequestHandler.java:234)
at org.apache.wicket.request.cycle.RequestCycle$HandlerExecutor.respond(RequestCycle.java:781)
at org.apache.wicket.request.RequestHandlerStack.execute(RequestHandlerStack.java:64)
at org.apache.wicket.request.cycle.RequestCycle.execute(RequestCycle.java:255)
at org.apache.wicket.request.cycle.RequestCycle.processRequest(RequestCycle.java:212)
at org.apache.wicket.request.cycle.RequestCycle.processRequestAndDetach(RequestCycle.java:283)
at org.apache.wicket.protocol.http.WicketFilter.processRequest(WicketFilter.java:185)
at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:241)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:600)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1703)
at java.lang.Thread.run(Thread.java:662)
Caused by:

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 eyesys.domain.Dimension3_$$_javassist_17.getUUID(Dimension3_$$_javassist_17.java)
at eyesys.web.invoice.invoice.lines2.AccountingLineRow$33.validate(AccountingLineRow.java:918)
at org.apache.wicket.markup.html.form.Form.validateFormValidator(Form.java:1826)
at org.apache.wicket.markup.html.form.Form.validateFormValidators(Form.java:1839)
at org.apache.wicket.markup.html.form.Form$17.component(Form.java:1865)
at org.apache.wicket.markup.html.form.Form$17.component(Form.java:1853)
at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:273)
at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:261)
at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:261)
at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:261)
at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:261)
at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:261)
at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:261)
at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:261)
at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:261)
at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:261)
at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:261)
at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:261)
at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:261)
at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:261)
at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:261)
at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:261)
at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:261)
at org.apache.wicket.util.visit.Visits.visitPostOrder(Visits.java:244)
at org.apache.wicket.markup.html.form.Form.validateNestedForms(Form.java:1851)
at org.apache.wicket.markup.html.form.Form.validate(Form.java:1715)
at org.apache.wicket.markup.html.form.Form.process(Form.java:803)
at org.apache.wicket.markup.html.form.Form.onFormSubmitted(Form.java:762)
at org.apache.wicket.ajax.form.AjaxFormSubmitBehavior.onEvent(AjaxFormSubmitBehavior.java:158)
at org.apache.wicket.ajax.AjaxEventBehavior.respond(AjaxEventBehavior.java:184)
at org.apache.wicket.ajax.AbstractDefaultAjaxBehavior.onRequest(AbstractDefaultAjaxBehavior.java:369)
at sun.reflect.GeneratedMethodAccessor78.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.wicket.RequestListenerInterface.internalInvoke(RequestListenerInterface.java:260)
at org.apache.wicket.RequestListenerInterface.invoke(RequestListenerInterface.java:241)
at org.apache.wicket.request.handler.ListenerInterfaceRequestHandler.invokeListener(ListenerInterfaceRequestHandler.java:255)
at org.apache.wicket.request.handler.ListenerInterfaceRequestHandler.respond(ListenerInterfaceRequestHandler.java:234)
at org.apache.wicket.request.cycle.RequestCycle$HandlerExecutor.respond(RequestCycle.java:781)
at org.apache.wicket.request.RequestHandlerStack.execute(RequestHandlerStack.java:64)
at org.apache.wicket.request.cycle.RequestCycle.execute(RequestCycle.java:255)
at org.apache.wicket.request.cycle.RequestCycle.processRequest(RequestCycle.java:212)
at org.apache.wicket.request.cycle.RequestCycle.processRequestAndDetach(RequestCycle.java:283)
at org.apache.wicket.protocol.http.WicketFilter.processRequest(WicketFilter.java:185)
at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:241)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:600)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1703)
at java.lang.Thread.run(Thread.java:662)

AccountingLineRow = DimensionHolder。

维度 3 = 维度。

最佳答案

据我所知,无论您调用什么方法,代理都已初始化。这样做可能有充分的理由。例如,您的 getId() 方法可以包含以下指令:

public void getId() {
LOG.debug("getId() called for entity with ID " + this.id + " and name " + name);
return this.id;
}

如果代理未初始化,则记录的名称将为空,这是不正确的。

我在某处读到,如果您使用属性访问类型而不是字段访问类型(即,如果您的 getter 被注释为映射注释评级者而不是您的字段)。

但我会在关闭 session 之前简单地初始化对象,而不是在之后通过发出新的 session 和新的数据库查询来初始化它。

关于java - 访问@Id 时出现 LazyInitializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15132191/

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