标签:could not initialize no session initialize proxy hibernate3
hibernate3中出现 could not initialize proxy - no Session 错误的解决办法以及lazy load的介绍 could not initialize proxy - no Session解决方法:<many-to-one> 和 <set> 中设置 lazy="false"
hbm.xml配置如下:
<?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">
<hibernate-mapping>
<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="10" />
</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>
</hibernate-mapping>
导致异常的原因:
使用HibernateTemplate的save、load等方法时,它的Session管理策略是:打开session,进行相关操作,默认使用lazy加载结果,不会发出sql语句,返回的是代理类,然后关闭session,此时你得到了一个Orgnization的代理类,如果你要调用Orgnization实例的getter方法,那么它就会去实际加载Orgnization对象,发出sql语句,可此时session已经关闭,因此会抛出异常,提示ould not initialize proxy - no Session。
版权声明:本文为博主原创文章,未经博主允许不得转载。
could not initialize proxy - no Session
标签:could not initialize no session initialize proxy hibernate3
原文地址:http://blog.csdn.net/han_ying_ying/article/details/46888589