-->
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.  [ 8 posts ] 
Author Message
 Post subject: LazyInitializationException / HQL
PostPosted: Fri Aug 07, 2009 2:01 am 
Beginner
Beginner

Joined: Wed Dec 10, 2008 5:59 am
Posts: 47
Hi,

I've got an HQL query which simply retrievies a few elements and their collections. I eagerly fetch those collections, and it
all works just fine.

Code:
select distinct o from Orders o left join fetch o.items i left join fetch i.product p


However, i certain scenarios i need to create a sort of "filter" so that only items that are products of a certain type are returned. I would usually try to do this by appying a "where" clause.

Code:
where p.type in (100, 500, 700)


The problem is that whenever i apply this "where" condition, collections are not longer eagerly fetched. Does anyone have any good ideas for how i can get around this? I have also tried the excact same thing with Criteria, but FetchMode.JOIN apparently gets ignored when introducing createCriteria() - so that didn't really help me either.


Top
 Profile  
 
 Post subject: Re: LazyInitializationException / HQL
PostPosted: Fri Aug 07, 2009 2:57 am 
Newbie

Joined: Fri Aug 07, 2009 1:02 am
Posts: 4
Using criteria you can cahive this. create alias for that collection as below

Code:
List cats = session.createCriteria(Cat.class)
     .createAlias("kittens", "kit")
     .add( Restrictions.like("kit.name", "Iz%") )
     .list();


if i got your question correctly it hepls you :-)


Top
 Profile  
 
 Post subject: Re: LazyInitializationException / HQL
PostPosted: Fri Aug 07, 2009 3:36 am 
Beginner
Beginner

Joined: Wed Dec 10, 2008 5:59 am
Posts: 47
No, not really. I've been over all those tutorials and javadocs that you refer to by your example - but they don't really give an answer.

The thing is that the Criteria query you refer to (from the Criteria API) doesn't necessarily fetch the "kittens" collection. I need
to both fetch a collection as well as "filter" on it. However, it seems that whenever one tries to apply "where" clauses to a
collection, eager fetching is ignored by Hibernate.

I would really loved to be proved wrong though.


Top
 Profile  
 
 Post subject: Re: LazyInitializationException / HQL
PostPosted: Fri Aug 07, 2009 7:21 am 
Beginner
Beginner

Joined: Wed Jul 29, 2009 3:43 pm
Posts: 22
Try this
select distinct o , i from Orders o join o.items i left join i.product p


Top
 Profile  
 
 Post subject: Re: LazyInitializationException / HQL
PostPosted: Fri Aug 07, 2009 8:17 am 
Beginner
Beginner

Joined: Wed Dec 10, 2008 5:59 am
Posts: 47
Unfortunately not. I believe that query doesn't eagerly fetch any collections either?


Top
 Profile  
 
 Post subject: Re: LazyInitializationException / HQL
PostPosted: Fri Aug 07, 2009 8:27 am 
Beginner
Beginner

Joined: Wed Dec 10, 2008 5:59 am
Posts: 47
Unfortunately not. I believe that query doesn't eagerly fetch any collections either?

This is really getting on my nerves. Two days without getting anywhere with something that should have been a rather simple thing?

To sum things up, i would like to get all Orders that has Items that has Products that is of a certain product type. Additionally,
the items collection for each order should be eagerly fetched, along with the product that is associated with each item.

The SQL query which does just this would be:

Code:
SELECT * FROM order
LEFT JOIN order_item ON order_item.order_id = order.id
LEFT JOIN item ON item.id = order_item.item_id
LEFT JOIN product ON product.id = item.product_id
WHERE product.type IN (100, 500, 700)
AND order.id = '62899ed4-df9a-4b70-917b-4d5d7b674a93'


Top
 Profile  
 
 Post subject: Re: LazyInitializationException / HQL
PostPosted: Fri Aug 07, 2009 9:37 am 
Beginner
Beginner

Joined: Wed Jul 29, 2009 3:43 pm
Posts: 22
Can you post your schema , mapping files and mapped classes??


Top
 Profile  
 
 Post subject: Re: LazyInitializationException / HQL
PostPosted: Wed Aug 12, 2009 2:16 am 
Beginner
Beginner

Joined: Wed Dec 10, 2008 5:59 am
Posts: 47
I finally solved it. The problem occured because i needed to specify the join type in my HQL.

So, it would be:

Code:
Criteria c = sess.createCriteria(Order.class)
  .createCriteria("items", JoinFragment.LEFT_OUTER_JOIN)
  .createCriteria("product").add(Restrictions.in("type", new Integer[] {100, 500, 700}));
c.setFetchMode("items", FetchMode.JOIN);
c.setFetchMode("items.product", FetchMode.JOIN);
c.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
c.list();


http://opensource.atlassian.com/projects/hibernate/browse/HHH-3524?focusedCommentId=33727&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_33727


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.