背景:
Customer和Order是一对多双向关联关系
当我们取到Customer对象后,把Session关闭,当要使用和该Customer对象关联的Order时,直接调用getOrders()方法导航到Order对象会得到异常。如下:
public static void main(String[] args) {
Customer customer = (Customer)getObject(Customer.class, new Long(26));
System.out.println("customer is detached");
for (Iterator it = customer.getOrders().iterator(); it.hasNext();) {
Order o = (Order)it.next();
System.out.println("Order Id=" + o.getId() + " orderNo=" + o.getOrderNo());
}
}
public static Object getObject(Class clazz, Long id) {
Session session = HibernateUtil.getSession();
Object obj = session.get(clazz, id);
session.close();
return obj;
}
运行以上代码,会得到以下异常。在Session关闭之后,无法取得延迟关联对象。
Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.Customer.orders, no session
or session was closed
解决以上问题,写一个通用方法取延迟关联对象(我写在HibernateUtil类中)
public static void findLazyObjects(Object main, Object proxy) {
if (!Hibernate.isInitialized(proxy)) {
Session session = getSession();
session.update(main);
Hibernate.initialize(proxy);
session.close();
}
}
修改main方法如下
public static void main(String[] args) {
Customer customer = (Customer)getObject(Customer.class, new Long(26));
System.out.println("customer is detached");
//初始化关联对象
HibernateUtil.initLazyObjects(customer, customer.getOrders());
for (Iterator it = customer.getOrders().iterator(); it.hasNext();) {
Order o = (Order)it.next();
System.out.println("Order Id=" + o.getId() + " orderNo=" + o.getOrderNo());
}
}
/**Output:
Hibernate: select customer0_.ID as ID1_0_, customer0_.NAME as NAME1_0_ from CUSTOMERS customer0_ where customer0_.ID=?
customer is detached
Hibernate: select orders0_.CUSTOMER_ID as CUSTOMER3_1_1_, orders0_.ID as ID1_, orders0_.ID as ID2_0_, orders0_.ORDER_NO as ORDER2_2_0_,
orders0_.CUSTOMER_ID as CUSTOMER3_2_0_, orders0_.PRICE as PRICE2_0_ from ORDERS orders0_ where orders0_.CUSTOMER_ID=?
Order Id=25 orderNo=no_002
Order Id=26 orderNo=no_003
Order Id=24 orderNo=no_001
*/
Customer和Order是一对多双向关联关系
当我们取到Customer对象后,把Session关闭,当要使用和该Customer对象关联的Order时,直接调用getOrders()方法导航到Order对象会得到异常。如下:
public static void main(String[] args) {
Customer customer = (Customer)getObject(Customer.class, new Long(26));
System.out.println("customer is detached");
for (Iterator it = customer.getOrders().iterator(); it.hasNext();) {
Order o = (Order)it.next();
System.out.println("Order Id=" + o.getId() + " orderNo=" + o.getOrderNo());
}
}
public static Object getObject(Class clazz, Long id) {
Session session = HibernateUtil.getSession();
Object obj = session.get(clazz, id);
session.close();
return obj;
}
运行以上代码,会得到以下异常。在Session关闭之后,无法取得延迟关联对象。
Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.Customer.orders, no session
or session was closed
解决以上问题,写一个通用方法取延迟关联对象(我写在HibernateUtil类中)
public static void findLazyObjects(Object main, Object proxy) {
if (!Hibernate.isInitialized(proxy)) {
Session session = getSession();
session.update(main);
Hibernate.initialize(proxy);
session.close();
}
}
修改main方法如下
public static void main(String[] args) {
Customer customer = (Customer)getObject(Customer.class, new Long(26));
System.out.println("customer is detached");
//初始化关联对象
HibernateUtil.initLazyObjects(customer, customer.getOrders());
for (Iterator it = customer.getOrders().iterator(); it.hasNext();) {
Order o = (Order)it.next();
System.out.println("Order Id=" + o.getId() + " orderNo=" + o.getOrderNo());
}
}
/**Output:
Hibernate: select customer0_.ID as ID1_0_, customer0_.NAME as NAME1_0_ from CUSTOMERS customer0_ where customer0_.ID=?
customer is detached
Hibernate: select orders0_.CUSTOMER_ID as CUSTOMER3_1_1_, orders0_.ID as ID1_, orders0_.ID as ID2_0_, orders0_.ORDER_NO as ORDER2_2_0_,
orders0_.CUSTOMER_ID as CUSTOMER3_2_0_, orders0_.PRICE as PRICE2_0_ from ORDERS orders0_ where orders0_.CUSTOMER_ID=?
Order Id=25 orderNo=no_002
Order Id=26 orderNo=no_003
Order Id=24 orderNo=no_001
*/