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>

[color=red][b]要注意配置过滤器的顺序:要把OpenSessionInviewFilter的过滤器放在最前面;同时要配置事务处理,否则会导致session处于只读状态而不能做修改,删除操作。[/b][/color]
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了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
org.hibernate.LazyInitializationException是一个在Hibernate中常见的异常。它表示在尝试访问延迟加载的属性或关联对象时,Hibernate会话已经关闭或不可用,导致无法初始化代理对象。 通常情况下,Hibernate会使用懒加载机制延迟加载关联对象。这意味着当你读取一个实体对象时,它的关联对象并不会立即加载,而是在需要访问关联对象时才会从数据库中加载。然而,如果在Session关闭后尝试访问延迟加载的属性或关联对象,就会抛出org.hibernate.LazyInitializationException异常。 解决org.hibernate.LazyInitializationException的方法有几种: 1. 保持会话打开:一种常见的解决方法是在访问延迟加载属性或关联对象之前保持Hibernate会话打开状态。这可以通过在事务中延长会话的生命周期或者在需要时重新加载实体对象来实现。这样做的目的是确保在访问延迟加载属性或关联对象时,会话仍然有效。 2. 使用Eager加载:可以将关联对象的加载方式从延迟加载改为立即加载(Eager加载)。这样做可以在加载实体对象时同时将关联对象一起加载,避免在访问时出现LazyInitializationException异常。但需要注意的是,如果关联对象的数据量很大或者关联对象之间存在循环引用,使用Eager加载可能会导致性能问题。 3. 手动初始化关联对象:在某些情况下,你可以在Hibernate会话仍然打开时手动初始化延迟加载的关联对象,以避免LazyInitializationException异常。你可以通过调用关联对象的getter方法或使用Hibernate.initialize()方法来实现。 需要根据具体情况选择合适的解决方法。如果你在使用Hibernate时遇到了org.hibernate.LazyInitializationException异常,可以根据上述方法尝试解决该异常
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值