-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: The Join fetch which throws a lazy exception
PostPosted: Mon Jun 12, 2006 5:00 am 
Newbie

Joined: Fri Jun 09, 2006 10:39 am
Posts: 4
Hello,

Hibernate throws a lazy exception even if I used setFetchMode(..., FetchMode.JOIN).

More in details :

Entities :
Code:
Underlying ---(productList)---> (one-to-many) Product


We need to retrieve the underlyings and all associated products :
Code:
Criteria criteria = session.createCriteria( Underlying.class );
criteria.setFetchMode("productList", FetchMode.JOIN);
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); // Remove duplicates underlyings


Here, the query is working : the products are retreived (no lazy exception).

But we also need to order by product maturity, so we added to the previous statement :
Code:
Criteria critProduct = criteria.createCriteria("productList", "productList");
critProduct.addOrder( Order.asc("maturity") );

With this order by, the the products stop being retrieved and Hibernate throws a lazy exception.

The question is simple : Does Hibernate forbid the use of the fetched entities in the order by clause ?
At first sight, it seems that using an order-by on the fetched entities (setFetchMode("productList", FetchMode.JOIN) cancels the fetch join, as if it wasn't done (the products are not loaded).


Info:

Hibernate version: 3.1.2
The generated SQL:
Code:
select
this_.UNDERLYING_ID as UNDERLYING1_49_1_,
this_.LABEL as LABEL49_1_,
productlis1_.PRODUCT_ID as PRODUCT1_46_0_,
productlis1_.MATURITY as MATURITY46_0_
from
UNDERLYING this_
inner join
PRODUCTS productlis1_
on this_.UNDERLYING_ID=productlis1_.UNDERLYING_ID
order by
productlis1_.MATURITY asc


Full stack trace of any exception that occurs:
Code:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.calyon.warrants.model.Underlying.productList, no session or session was closed



Thanks for your help,

Tom


Last edited by tomq on Tue Jun 13, 2006 12:12 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 12:11 pm 
Newbie

Joined: Fri Jun 09, 2006 10:39 am
Posts: 4
Any ideas ?
Is the answer so simple (RTFM ;-) ) ?

Currently, underlyings and products are fetched (inner join) but there is still the Lazy Exception when accessing the productList from an underlying.

Any help is welcome.

Regards,

Tom


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 2:30 pm 
Regular
Regular

Joined: Mon May 22, 2006 2:30 pm
Posts: 74
When are you attempting to access the productList? The error is that there is no session associated with the current thread at the time the productList collection is loaded. You could try setting lazy to true just to see if it clears up the error. If it does, that would indicate that your session management approach has some problem. You normally see this error when an object is loaded while a session is available, but a collection of persistent objects assocaited with that object was not loaded at that time. Later, when code attempts to access the collection, Hibernate attempts to retrieve the persistent objects in the collection from the database, but there is no current session at that point.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 14, 2006 8:38 am 
Newbie

Joined: Fri Jun 09, 2006 10:39 am
Posts: 4
The session has been closed when we accessed the 'productList' field. This is why we need to pre-load the products (the joinfetch) when the session is still opened.

If the session is open when we access the 'productList', Hibernate runs the corresponding queries for getting the products for each underlyings. This is the known n+1 select in this case : on select for all underlyings, one select for each product list of each underlying.

So, why the join fetch on "productList" is "disabled" if a Criteria on "productList" is added ?

Thanks,

Tom


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 16, 2006 8:00 am 
Newbie

Joined: Fri Jun 09, 2006 10:39 am
Posts: 4
Any Idea on why a join fetch is "disabled" if a Criteria is created on this join fetch ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.