一种看起来low的方式:在方法上面加上boolean类型参数用来判断是否进行初始化
static Employee query(int id,boolean includeDepart){ Session s=null; try{ s=HibernateUtil.getSession(); Employee emp=(Employee) s.get(Employee.class, id); if(includeDepart) Hibernate.initialize(emp.getDepart()); return emp; }finally{ if(s!=null) s.close(); } }
时间: 2024-10-13 17:44:30