org.hibernate.LazyInitializationException异常及解决方案
程序员文章站
2022-03-02 14:10:06
...
1.基本配置信息简介
1)有个PsEmployee的类
2)有个MsdRoleEmployee类
3)PsEmployee类与MsdRoleEmployee是多对多的关联关系
4)PsEmployee类的映射文件PsEmployee.hbm.xml如下:
5)MsdRoleEmployee类的映射文件MsdRoleEmployee.hbm.xml如下
2.当我在业务层根据EWein查找PsEmployee的时候,程序就会报告异常信息:
org.hibernate.LazyInitializationException
3.解决org.hibernate.LazyInitializationException的方法:
1):将Lazy设置为false,即:
2)使用OpenSessionInViewFilter的过滤器
在web.xml中添加配置信息:
[color=red][b]要注意配置过滤器的顺序:要把OpenSessionInviewFilter的过滤器放在最前面;同时要配置事务处理,否则会导致session处于只读状态而不能做修改,删除操作。[/b][/color]
3)在session没有关闭之前,把要查询的数据加载出来,存到对象或者容器中,等用到的时候直接的取出来就OK了。
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了。