2

我们在 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 超类中,并且我们正在使用的当前Dimension 实例是来自休眠的代理对象?这在我们的耳朵里听起来也很奇怪,所以也许某个善良的灵魂可以阐明我们的问题?

谢谢!

编辑:这是堆栈跟踪:

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 = 维度。

4

1 回答 1

1

AFAIK,代理已初始化,无论您调用什么方法。这样做可能有充分的理由。例如,您的getId()方法可能包含以下说明:

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

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

我在某处读到,getId()如果您使用属性访问类型而不是字段访问类型(即,如果您的 getter 使用映射注释而不是字段进行注释),则调用时代理未初始化。

但是我会在关闭会话之前简单地初始化对象,而不是在之后初始化它,方法是发出一个新的 sessio 和一个新的数据库查询。

于 2013-02-28T13:41:22.293 回答