MyException - 我的异常网
当前位置:我的异常网» 软件架构设计 » org.hibernate.LazyInitializationException错误及

org.hibernate.LazyInitializationException错误及解决方案

www.MyException.Cn  网友分享于:2013-10-08  浏览:8次
org.hibernate.LazyInitializationException异常及解决方案
1.基本配置信息简介
1)有个PsEmployee的类
public class PsEmployee implements java.io.Serializable {

	private Long EWein;

	private String name;

	private String acname;

	private String grade;

	private String deptid;

	private Long SWein;

	private Long SGoa;

	private String co;

	private String unit;

	private Long PNum;

	private String regTemp;

	private Long reportsTo;

	private String SName;

	private String dept;

	private Long state;

	private Long importflag;

	private String isid;

	private String email;

	private String win;

	private Long tempflag;

	private String mendorid;

	private String modifyDate;

	private String SWeinD;

	private Set msdRoleEmployees = new HashSet(0);
	
	//add by alan 2010-05-26
	private String employeeType;
	
	// Constructors

	/** default constructor */
	public PsEmployee() {
	}

	/** minimal constructor */
	public PsEmployee(Long EWein) {
		this.EWein = EWein;
	}

	/** full constructor */
	public PsEmployee(Long EWein, String name, String acname, String grade,
			String deptid, Long SWein, Long SGoa, String co, String unit,
			Long PNum, String regTemp, Long reportsTo, String SName,
			String dept, Long state, Long importflag, String isid,
			String email, String win, Long tempflag, String mendorid,
			String modifyDate, String SWeinD, Set msdRoleEmployees) {
		this.EWein = EWein;
		this.name = name;
		this.acname = acname;
		this.grade = grade;
		this.deptid = deptid;
		this.SWein = SWein;
		this.SGoa = SGoa;
		this.co = co;
		this.unit = unit;
		this.PNum = PNum;
		this.regTemp = regTemp;
		this.reportsTo = reportsTo;
		this.SName = SName;
		this.dept = dept;
		this.state = state;
		this.importflag = importflag;
		this.isid = isid;
		this.email = email;
		this.win = win;
		this.tempflag = tempflag;
		this.mendorid = mendorid;
		this.modifyDate = modifyDate;
		this.SWeinD = SWeinD;
		this.msdRoleEmployees = msdRoleEmployees;
	}

	// Property accessors

	public Long getEWein() {
		return this.EWein;
	}

	public void setEWein(Long EWein) {
		this.EWein = EWein;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAcname() {
		return this.acname;
	}

	public void setAcname(String acname) {
		this.acname = acname;
	}

	public String getGrade() {
		return this.grade;
	}

	public void setGrade(String grade) {
		this.grade = grade;
	}

	public String getDeptid() {
		return this.deptid;
	}

	public void setDeptid(String deptid) {
		this.deptid = deptid;
	}

	public Long getSWein() {
		return this.SWein;
	}

	public void setSWein(Long SWein) {
		this.SWein = SWein;
	}

	public Long getSGoa() {
		return this.SGoa;
	}

	public void setSGoa(Long SGoa) {
		this.SGoa = SGoa;
	}

	public String getCo() {
		return this.co;
	}

	public void setCo(String co) {
		this.co = co;
	}

	public String getUnit() {
		return this.unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	public Long getPNum() {
		return this.PNum;
	}

	public void setPNum(Long PNum) {
		this.PNum = PNum;
	}

	public String getRegTemp() {
		return this.regTemp;
	}

	public void setRegTemp(String regTemp) {
		this.regTemp = regTemp;
	}

	public Long getReportsTo() {
		return this.reportsTo;
	}

	public void setReportsTo(Long reportsTo) {
		this.reportsTo = reportsTo;
	}

	public String getSName() {
		return this.SName;
	}

	public void setSName(String SName) {
		this.SName = SName;
	}

	public String getDept() {
		return this.dept;
	}

	public void setDept(String dept) {
		this.dept = dept;
	}

	public Long getState() {
		return this.state;
	}

	public void setState(Long state) {
		this.state = state;
	}

	public Long getImportflag() {
		return this.importflag;
	}

	public void setImportflag(Long importflag) {
		this.importflag = importflag;
	}

	public String getIsid() {
		return this.isid;
	}

	public void setIsid(String isid) {
		this.isid = isid;
	}

	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getWin() {
		return this.win;
	}

	public void setWin(String win) {
		this.win = win;
	}

	public Long getTempflag() {
		return this.tempflag;
	}

	public void setTempflag(Long tempflag) {
		this.tempflag = tempflag;
	}

	public String getMendorid() {
		return this.mendorid;
	}

	public void setMendorid(String mendorid) {
		this.mendorid = mendorid;
	}

	public String getModifyDate() {
		return this.modifyDate;
	}

	public void setModifyDate(String modifyDate) {
		this.modifyDate = modifyDate;
	}

	public String getSWeinD() {
		return this.SWeinD;
	}

	public void setSWeinD(String SWeinD) {
		this.SWeinD = SWeinD;
	}

	public Set getMsdRoleEmployees() {
		return this.msdRoleEmployees;
	}

	public void setMsdRoleEmployees(Set msdRoleEmployees) {
		this.msdRoleEmployees = msdRoleEmployees;
	}

	public String getEmployeeType() {
		return employeeType;
	}

	public void setEmployeeType(String employeeType) {
		this.employeeType = employeeType;
	}

2)有个MsdRoleEmployee类
public class MsdRoleEmployee implements java.io.Serializable {

	// Fields

	private MsdRoleEmployeeId id;

	private Long emWin;

	private String roleId;

	private MsdRole msdRole;

	private PsEmployee psEmployee;

	// Constructors

	/** default constructor */
	public MsdRoleEmployee() {
	}

	/** full constructor */
	public MsdRoleEmployee(MsdRoleEmployeeId id, MsdRole msdRole,
			PsEmployee psEmployee) {
		this.id = id;
		this.msdRole = msdRole;
		this.psEmployee = psEmployee;
	}

	// Property accessors

	public MsdRoleEmployeeId getId() {
		return this.id;
	}

	public void setId(MsdRoleEmployeeId id) {
		this.id = id;
	}

	public MsdRole getMsdRole() {
		return this.msdRole;
	}

	public void setMsdRole(MsdRole msdRole) {
		this.msdRole = msdRole;
	}

	public PsEmployee getPsEmployee() {
		return this.psEmployee;
	}

	public void setPsEmployee(PsEmployee psEmployee) {
		this.psEmployee = psEmployee;
	}

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public Long getEmWin() {
		return emWin;
	}

	public void setEmWin(Long emWin) {
		this.emWin = emWin;
	}

3)PsEmployee类与MsdRoleEmployee是多对多的关联关系
4)PsEmployee类的映射文件PsEmployee.hbm.xml如下:
 <hibernate-mapping>
    <class name="com.synchrobit.msd.model.po.PsEmployee" table="PS_EMPLOYEE">
        <id name="EWein" type="java.lang.Long">
            <column name="E_WEIN" precision="8" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" length="50" />
        </property>
        <property name="acname" type="java.lang.String">
            <column name="ACNAME" length="100" />
        </property>
        <property name="grade" type="java.lang.String">
            <column name="GRADE" length="20" />
        </property>
        <property name="deptid" type="java.lang.String">
            <column name="DEPTID" length="100" />
        </property>
        <property name="SWein" type="java.lang.Long">
            <column name="S_WEIN" precision="8" scale="0" />
        </property>
        <property name="SGoa" type="java.lang.Long">
            <column name="S_GOA" precision="8" scale="0" />
        </property>
        <property name="co" type="java.lang.String">
            <column name="CO" length="100" />
        </property>
        <property name="unit" type="java.lang.String">
            <column name="UNIT" length="100" />
        </property>
        <property name="PNum" type="java.lang.Long">
            <column name="P_NUM" precision="15" scale="0" />
        </property>
        <property name="regTemp" type="java.lang.String">
            <column name="REG_TEMP" length="100" />
        </property>
        <property name="reportsTo" type="java.lang.Long">
            <column name="REPORTS_TO" precision="15" scale="0" />
        </property>
        <property name="SName" type="java.lang.String">
            <column name="S_NAME" length="100" />
        </property>
        <property name="dept" type="java.lang.String">
            <column name="DEPT" length="100" />
        </property>
        <property name="state" type="java.lang.Long">
            <column name="STATE" precision="22" scale="0" />
        </property>
        <property name="importflag" type="java.lang.Long">
            <column name="IMPORTFLAG" precision="22" scale="0" />
        </property>
        <property name="isid" type="java.lang.String">
            <column name="ISID" length="100" />
        </property>
        <property name="email" type="java.lang.String">
            <column name="EMAIL" length="100" />
        </property>
        <property name="win" type="java.lang.String">
            <column name="WIN" length="20" />
        </property>
        <property name="tempflag" type="java.lang.Long">
            <column name="TEMPFLAG" precision="22" scale="0" />
        </property>
        <property name="mendorid" type="java.lang.String">
            <column name="MENDORID" length="50" />
        </property>
        <property name="modifyDate" type="java.lang.String">
            <column name="MODIFY_DATE" length="50" />
        </property>
        <property name="SWeinD" type="java.lang.String">
            <column name="S_WEIN_D" length="10" />
        </property>
        <property name="employeeType" type="java.lang.String">
            <column name="EMPLOYEE_TYPE" length="10" />
        </property>
        <set name="msdRoleEmployees" inverse="true">
            <key>
                <column name="E_WEIN" precision="8" scale="0" not-null="true" />
            </key>
            <one-to-many class="com.synchrobit.msd.model.po.MsdRoleEmployee" />
        </set>
    </class>
</hibernate-mapping>

5)MsdRoleEmployee类的映射文件MsdRoleEmployee.hbm.xml如下
<hibernate-mapping>
    <class name="com.synchrobit.msd.model.po.MsdRoleEmployee" table="MSD_ROLE_EMPLOYEE" >
        <composite-id name="id" class="com.synchrobit.msd.model.po.MsdRoleEmployeeId">
            <key-property name="rid" type="java.lang.String">
                <column name="RID" length="20" />
            </key-property>
            <key-property name="EWein" type="java.lang.Long">
                <column name="E_WEIN" precision="8" scale="0" />
            </key-property>
        </composite-id>
        <many-to-one name="msdRole" class="com.synchrobit.msd.model.po.MsdRole" update="false" insert="false" fetch="select">
            <column name="RID" length="20" not-null="true" />
        </many-to-one>
        <many-to-one name="psEmployee" class="com.synchrobit.msd.model.po.PsEmployee" update="false" insert="false" fetch="select">
            <column name="E_WEIN" precision="8" scale="0" not-null="true" />
        </many-to-one>
    </class>
</hibernate-mapping>

2.当我在业务层根据EWein查找PsEmployee的时候,程序就会报告异常信息:
  org.hibernate.LazyInitializationException
3.解决org.hibernate.LazyInitializationException的方法:
1):将Lazy设置为false,即:
<hibernate-mapping>
    <class name="com.synchrobit.msd.model.po.PsEmployee" table="PS_EMPLOYEE">
        <id name="EWein" type="java.lang.Long">
            <column name="E_WEIN" precision="8" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" length="50" />
        </property>
        <property name="acname" type="java.lang.String">
            <column name="ACNAME" length="100" />
        </property>
        <property name="grade" type="java.lang.String">
            <column name="GRADE" length="20" />
        </property>
        <property name="deptid" type="java.lang.String">
            <column name="DEPTID" length="100" />
        </property>
        <property name="SWein" type="java.lang.Long">
            <column name="S_WEIN" precision="8" scale="0" />
        </property>
        <property name="SGoa" type="java.lang.Long">
            <column name="S_GOA" precision="8" scale="0" />
        </property>
        <property name="co" type="java.lang.String">
            <column name="CO" length="100" />
        </property>
        <property name="unit" type="java.lang.String">
            <column name="UNIT" length="100" />
        </property>
        <property name="PNum" type="java.lang.Long">
            <column name="P_NUM" precision="15" scale="0" />
        </property>
        <property name="regTemp" type="java.lang.String">
            <column name="REG_TEMP" length="100" />
        </property>
        <property name="reportsTo" type="java.lang.Long">
            <column name="REPORTS_TO" precision="15" scale="0" />
        </property>
        <property name="SName" type="java.lang.String">
            <column name="S_NAME" length="100" />
        </property>
        <property name="dept" type="java.lang.String">
            <column name="DEPT" length="100" />
        </property>
        <property name="state" type="java.lang.Long">
            <column name="STATE" precision="22" scale="0" />
        </property>
        <property name="importflag" type="java.lang.Long">
            <column name="IMPORTFLAG" precision="22" scale="0" />
        </property>
        <property name="isid" type="java.lang.String">
            <column name="ISID" length="100" />
        </property>
        <property name="email" type="java.lang.String">
            <column name="EMAIL" length="100" />
        </property>
        <property name="win" type="java.lang.String">
            <column name="WIN" length="20" />
        </property>
        <property name="tempflag" type="java.lang.Long">
            <column name="TEMPFLAG" precision="22" scale="0" />
        </property>
        <property name="mendorid" type="java.lang.String">
            <column name="MENDORID" length="50" />
        </property>
        <property name="modifyDate" type="java.lang.String">
            <column name="MODIFY_DATE" length="50" />
        </property>
        <property name="SWeinD" type="java.lang.String">
            <column name="S_WEIN_D" length="10" />
        </property>
        <property name="employeeType" type="java.lang.String">
            <column name="EMPLOYEE_TYPE" length="10" />
        </property>
       <set name="msdRoleEmployees" inverse="true" lazy="false">
            <key>
                <column name="E_WEIN" precision="8" scale="0" not-null="true" />
            </key>
            <one-to-many class="com.synchrobit.msd.model.po.MsdRoleEmployee" />
        </set>
    </class>
</hibernate-mapping>

2)使用OpenSessionInViewFilter的过滤器
在web.xml中添加配置信息:
     <filter>
	    <filter-name>hibernateFilter</filter-name>
	    <filter-class>
	      org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
	    </filter-class>
	 </filter>
         <filter-mapping>
	     <filter-name>hibernateFilter</filter-name>
	     <url-pattern>/*</url-pattern>
	 </filter-mapping>

要注意配置过滤器的顺序:要把OpenSessionInviewFilter的过滤器放在最前面;同时要配置事务处理,否则会导致session处于只读状态而不能做修改,删除操作。
3)在session没有关闭之前,把要查询的数据加载出来,存到对象或者容器中,等用到的时候直接的取出来就OK了。
在具体的DAO层调用的方法:
public PsEmployee findByIdWithRoles(java.lang.Long id) {
		log.debug("getting PsEmployee instance with id: " + id);
		try {
			PsEmployee instance = (PsEmployee) getHibernateTemplate().get(
					"com.synchrobit.msd.model.po.PsEmployee", id);
		[color=red][b]Set tmp = new HashSet();[/b][/color]
			if (instance != null)
			{
				Iterator roles = instance.getMsdRoleEmployees().iterator();
				while (roles.hasNext())
				{
					MsdRoleEmployee tmpRole = (MsdRoleEmployee)roles.next();
					tmp.add(tmpRole);
					
					tmpRole.getMsdRole().getRid();
				}
			}
	        instance.setMsdRoleEmployees(tmp);//将查询的数据保存到对象中,以便后面使用。
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
在Service层直接的调用DAO层返回的PsEmployee对象再查找数据就OK了。

文章评论

10个帮程序员减压放松的网站
10个帮程序员减压放松的网站
如何区分一个程序员是“老手“还是“新手“?
如何区分一个程序员是“老手“还是“新手“?
什么才是优秀的用户界面设计
什么才是优秀的用户界面设计
程序员周末都喜欢做什么?
程序员周末都喜欢做什么?
老美怎么看待阿里赴美上市
老美怎么看待阿里赴美上市
不懂技术不要对懂技术的人说这很容易实现
不懂技术不要对懂技术的人说这很容易实现
“懒”出效率是程序员的美德
“懒”出效率是程序员的美德
那些争议最大的编程观点
那些争议最大的编程观点
聊聊HTTPS和SSL/TLS协议
聊聊HTTPS和SSL/TLS协议
程序员最害怕的5件事 你中招了吗?
程序员最害怕的5件事 你中招了吗?
我是如何打败拖延症的
我是如何打败拖延症的
为啥Android手机总会越用越慢?
为啥Android手机总会越用越慢?
我跳槽是因为他们的显示器更大
我跳槽是因为他们的显示器更大
程序员和编码员之间的区别
程序员和编码员之间的区别
程序员必看的十大电影
程序员必看的十大电影
“肮脏的”IT工作排行榜
“肮脏的”IT工作排行榜
初级 vs 高级开发者 哪个性价比更高?
初级 vs 高级开发者 哪个性价比更高?
看13位CEO、创始人和高管如何提高工作效率
看13位CEO、创始人和高管如何提高工作效率
亲爱的项目经理,我恨你
亲爱的项目经理,我恨你
中美印日四国程序员比较
中美印日四国程序员比较
当下全球最炙手可热的八位少年创业者
当下全球最炙手可热的八位少年创业者
程序员的鄙视链
程序员的鄙视链
我的丈夫是个程序员
我的丈夫是个程序员
写给自己也写给你 自己到底该何去何从
写给自己也写给你 自己到底该何去何从
鲜为人知的编程真相
鲜为人知的编程真相
做程序猿的老婆应该注意的一些事情
做程序猿的老婆应该注意的一些事情
5款最佳正则表达式编辑调试器
5款最佳正则表达式编辑调试器
总结2014中国互联网十大段子
总结2014中国互联网十大段子
为什么程序员都是夜猫子
为什么程序员都是夜猫子
旅行,写作,编程
旅行,写作,编程
程序猿的崛起——Growth Hacker
程序猿的崛起——Growth Hacker
如何成为一名黑客
如何成为一名黑客
一个程序员的时间管理
一个程序员的时间管理
老程序员的下场
老程序员的下场
60个开发者不容错过的免费资源库
60个开发者不容错过的免费资源库
 程序员的样子
程序员的样子
要嫁就嫁程序猿—钱多话少死的早
要嫁就嫁程序猿—钱多话少死的早
程序员的一天:一寸光阴一寸金
程序员的一天:一寸光阴一寸金
10个调试和排错的小建议
10个调试和排错的小建议
程序员应该关注的一些事儿
程序员应该关注的一些事儿
Web开发者需具备的8个好习惯
Web开发者需具备的8个好习惯
团队中“技术大拿”并非越多越好
团队中“技术大拿”并非越多越好
每天工作4小时的程序员
每天工作4小时的程序员
Web开发人员为什么越来越懒了?
Web开发人员为什么越来越懒了?
程序员眼里IE浏览器是什么样的
程序员眼里IE浏览器是什么样的
软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有