Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: How to eager fetch a Hibernate proxy
PostPosted: Sat Oct 15, 2016 6:51 pm 
Newbie

Joined: Sat Oct 15, 2016 6:32 pm
Posts: 2
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?


Top
 Profile  
 
 Post subject: Re: Hibernate proxy cache confusion issue
PostPosted: Mon Oct 17, 2016 5:23 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 982
You need to use an HQL query like this one:

Code:
select o
from Order o
left join fetch p.product p
left join fetch p.supplier
where o.id = :orderId

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: Hibernate proxy cache confusion issue
PostPosted: Tue Oct 18, 2016 11:27 am 
Newbie

Joined: Sat Oct 15, 2016 6:32 pm
Posts: 2
vlad wrote:
You need to use an HQL query like this one:

Code:
select o
from Order o
left join fetch p.product p
left join fetch p.supplier
where o.id = :orderId



But unfortunately, we don't use the HSQL in the implementation.
And, another point, the issue occurs when fetch costs, I believe above HSQL will works well when fetch product and supplier.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.