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