懒加载异常LazyInitializationException:19

16:09:57,453 ERROR LazyInitializationException:19 - could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:60)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:172)
at com.zw.domain.Account$$EnhancerByCGLIB$$b9abda1e.getName(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:851)
at ognl.OgnlRuntime.getMethodValue(OgnlRuntime.java:1414)
at ognl.ObjectPropertyAccessor.getPossibleProperty(ObjectPropertyAccessor.java:60)
at ognl.ObjectPropertyAccessor.getProperty(ObjectPropertyAccessor.java:147)
at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.getProperty(ObjectAccessor.java:17)
at ognl.OgnlRuntime.getProperty(OgnlRuntime.java:2210)
at ognl.ASTProperty.getValueBody(ASTProperty.java:114)
at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
at ognl.SimpleNode.getValue(SimpleNode.java:258)
at ognl.ASTChain.getValueBody(ASTChain.java:141)
at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
at ognl.SimpleNode.getValue(SimpleNode.java:258)
at ognl.Ognl.getValue(Ognl.java:494)
at com.opensymphony.xwork2.ognl.OgnlUtil.getValue(OgnlUtil.java:206)
at com.opensymphony.xwork2.ognl.OgnlValueStack.findValue(OgnlValueStack.java:276)
at org.apache.struts2.components.Property.start(Property.java:141)
at org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(ComponentTagSupport.java:53)
at org.apache.jsp.find_jsp._jspx_meth_s_005fproperty_005f8(find_jsp.java:532)
at org.apache.jsp.find_jsp._jspx_meth_s_005fiterator_005f0(find_jsp.java:324)
at org.apache.jsp.find_jsp._jspService(find_jsp.java:205)

这是一个lazy使用后的Exception,使用迟时加载,在session(hibernate里的session),关闭后使用该对象的未加载变量,也就是说session已经关闭,没有保存到内存中,然后你使用了,导致该异常。在<many-to-one>中这错误很常见,字面意义就是不能被初始化,因为session已经关闭了。

解决方法:将多对一中的一那边xxx.hbm.xml中懒加载关掉 (多个学生对应一个管理者)

管理者:

<class name="Account" table="account"  lazy="false">
<id name="id" >
<generator class="native"/>
</id>
<property name="name"/>
<property name="passwd"/>
<set name="student">
<key column="accountid"></key>
<one-to-many class="Student"/>
</set>

</class>

学生:

<class name="Student" table="student" >
<id name="id" >
<generator class="native"/>
</id>
<property name="name"/>
<property name="sex"/>
<property name="age"/>
<property name="xh"/>
<property name="birthday"/>
<property name="sal"/>
<many-to-one name="account" column="accountid" />

</class>

以上仅是个人观点,希望可以对您有帮助,欢迎拍砖(*^_^*)/

原文地址:https://www.cnblogs.com/alvin-perfect/p/4402740.html