1、异常描述
org.hibernate.LazyInitializationException:
failed to lazily initialize a collection of role: cn.wang.entity.Clazz.studentSet, no session or session was closed
备注:
此处hibernate版本号:3.6.10.Final
2、示例代码
2-1 实体类
// 学生实体类
public class Student implements Serializable{
private Integer id;
private String name;
private Date birthday;
// 创建关系属性
private Clazz clazz;
// 省略相关代码...
}
// 班级表
public class Clazz implements Serializable{
private Integer id;
private String name;
// 建立关系属性
private Set<Student> studentSet = new HashSet<>();
// 省略部分代码...
2-2 Clazz.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hiberna