static Employee find(int id) {
Session s = null;
try {
s = HibernateSessionFactory.getSession();
Employee emp = (Employee) s.get(Employee.class, id);
//第一条 System.out.print(emp.getDepart());
//第2条 emp.getDepart();
return emp;
} finally {
if (s != null) {
s.close();
}
}
}
上面注释的2条代码,单用第一条时,2条SQL语句都出来了
单用第2条时,只出来一条SQL语句,剩下的一条懒加载,2条代码只是差个输出,为什么会出现不同结果
Session s = null;
try {
s = HibernateSessionFactory.getSession();
Employee emp = (Employee) s.get(Employee.class, id);
//第一条 System.out.print(emp.getDepart());
//第2条 emp.getDepart();
return emp;
} finally {
if (s != null) {
s.close();
}
}
}
上面注释的2条代码,单用第一条时,2条SQL语句都出来了
单用第2条时,只出来一条SQL语句,剩下的一条懒加载,2条代码只是差个输出,为什么会出现不同结果
加载了Depart对象