欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Hibernate no session 错误 博客分类: JAVA常用框架 HibernateJava 

程序员文章站 2024-02-26 10:05:52
...
org.hibernate.LazyInitializationException: could not initialize proxy - no Session

//员工类
public class Employee implements java.io.Serializable {
	private Integer empno;
	private Department department;
	private String ename;
	private String job;
	private Date hiredate;
	private Double sal;
	private String pictureuri;
	private Double maxsal;
	private Double minsal;
}
//部门类
public class Department implements java.io.Serializable {
	private Integer deptno;
	private String dname;
	private Set employees = new HashSet(0);
}

//查询所有员工
public List<Employee>findAll(){
		List <Employee> empList=getSession().createCriteria(Employee.class).createCriteria("department").list();
//后面一定得加上createCriteria("department")这个条件,Hibernate才会去关联的部门表中查询员工的部门信息!!!不加的话,当你遍历empList集合输出员工部门时就会报上边所列错误,因为员工表中存放的只是一个部门ID外键,Hibernate不会主动去查询!切记!!!
		return empList;
	}
相关标签: Hibernate Java