The entity hierarchy in my system is similar with following,
Code:
public class Supplier{
...
}
public class Product{
private Supplier supplier;
...
}
public class Cost{
private Supplier supplier;
...
}
public class Order{
private Long id;
private Product product;
private List<Cost> costs;
}
The max_fetch_depth is set to 1, and costs of Order is configured as lazy.
In the DAO layer, we use below code to load all the order info,
Code:
public Order loadOrderInfo(Long id){
Order order = getSession().get(Order.class, id);
Hibernate.initialize(order.getCosts());
}
The problem is that because max_fetch_depth is 1, so the Order's product's Supplier is proxy. But when call Hibernate.initialize the costs, the cost's supplier is proxy too if the cost's supplier id is same with product's Supplier id.
This incur the "session is closed exception" in the JSP view.
Our current fix is add one line Hibernate.initialize(order.getProduct().getSupplier());, but the code seems ugly, and there is addition SQL to fetch the Supplier.
Anyone have good solution for this?