public static void main(String[] args){
Department depart = add();
Employee emp = query(1);
//System.out.println("depart name:"+emp.getDepart().getName()); //出现在这会报错 懒加载1}
static Employee query(int empId){
Session s = null;
Transaction tx =null;
try{
s=HibernateUtil.getSession();
tx = s.beginTransaction();
Employee emp = (Employee)s.get(Employee.class,empId):
System.out.println("depart name:"+emp.getDepart().getName());
//避免懒加载错 Hibernate.initialize(emp.getDepart());//session对象关闭 使用该对象会异常。
tx.commit();}
疑问 : 在query方法中调用对象属性没问题 session关闭后 在外部调用抛出懒加载异常
get()方法也会懒加载吗????????
Department depart = add();
Employee emp = query(1);
//System.out.println("depart name:"+emp.getDepart().getName()); //出现在这会报错 懒加载1}
static Employee query(int empId){
Session s = null;
Transaction tx =null;
try{
s=HibernateUtil.getSession();
tx = s.beginTransaction();
Employee emp = (Employee)s.get(Employee.class,empId):
System.out.println("depart name:"+emp.getDepart().getName());
//避免懒加载错 Hibernate.initialize(emp.getDepart());//session对象关闭 使用该对象会异常。
tx.commit();}
疑问 : 在query方法中调用对象属性没问题 session关闭后 在外部调用抛出懒加载异常
get()方法也会懒加载吗????????
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货