访问@Id时出现LazyInitializationException

时间:2013-02-28 09:43:28

标签: java hibernate jpa lazy-loading

我们在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实例是来自hibernate的代理对象?这在我们的耳朵里听起来很奇怪,所以也许某种灵魂可以解释我们的问题?

谢谢!

编辑:这是stacktrace:

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。

Dimension3 = Dimension。

1 个答案:

答案 0 :(得分:1)

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

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

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

如果您使用属性访问类型而不是字段访问类型(例如,如果您的getter使用比您的字段更大的映射注释注释),我在某处调用{@ 1}}时未初始化代理。

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