• could not initialize proxy


    hibernate3中出现 could not initialize proxy - no Session 错误的解决的方法以及lazy load的介绍 could not initialize proxy - no Session
    异常:org.hibernate.LazyInitializationException: could not initialize proxy - no Session

    原因:hibernate3 many-to-one的默认选项是 lazy = "proxy"

    解决方法:<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。

  • 相关阅读:
    codeforces#1310B. Double Elimination(动态规划)
    codeforces#1300E. Water Balance(贪心)
    带权并查集
    codeforces#1295D. Same GCDs(数论/莫比乌斯)
    jsp实现图片换行显示
    codeforces#1257 F. Make Them Similar ( 经典中间相遇问题 )
    codeforces#1248D2. The World Is Just a Programming Task(括号匹配转化为折线处理)
    codeforces#1251E2. Voting (Hard Version)(贪心)
    codeforces#1249F. Maximum Weight Subset(树上dp)
    hdu6731 Angle Beats(ccpc秦皇岛A,计算几何)
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5352533.html
Copyright © 2020-2023  润新知