workaround for could not initialize proxy-no Session error in Hibernate3 and introduction of lazy load could not initialize Proxy-no Sessio N
Exception: Org.hibernate.LazyInitializationException:could not initialize proxy-no Session
Cause: The default option for Hibernate3 many-to-one is lazy = "Proxy"
Workaround:<many-to-one> and <set> set lazy= "false"
The Hbm.xml configuration is as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<class name= "Po. EMP "table=" EMP ">
<id name= "empno" type= "Java.lang.Short" >
<column name= "EMPNO" precision= "4" scale= "0"/>
<generator class= "Increment"/>
</id>
<property name= "ename" type= "java.lang.String" >
<column name= "ename" length= "ten"/>
</property>
<property name= "Job" type= "java.lang.String" >
<column name= "JOB" length= "9"/>
</property>
<property name= "Mgr" type= "Java.lang.Short" >
<column name= "MGR" precision= "4" scale= "0"/>
</property>
<property name= "HireDate" type= "Java.util.Date" >
<column name= "HireDate" length= "7"/>
</property>
<property name= "Sal" type= "java.lang.Double" >
<column name= "SAL" precision= "7"/>
</property>
<property name= "comm" type= "java.lang.Double" >
<column name= "COMM" precision= "7"/>
</property>
<many-to-one name= "Dept" class= "Po. Dept "column=" DEPTNO "></many-to-one>
</class>
Cause of the exception:
When using Hibernatetemplate's save, load, and other methods, its session management strategy is: open session, related operations, by default using lazy load results, no SQL statement, return the proxy class, and then close the session, At this point you get a orgnization proxy class, if you want to invoke the Orgnization instance of the getter method, then it will actually load Orgnization object, issued a SQL statement, can now be closed session, so will throw an exception, Tip Ould Not initialize Proxy-no Session.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Could not initialize Proxy-no Session