-->
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.  [ 6 posts ] 
Author Message
 Post subject: outer join lazy initialization
PostPosted: Wed Apr 07, 2004 2:37 pm 
Regular
Regular

Joined: Fri Jan 16, 2004 4:48 am
Posts: 56
Hello,

I have 3 tables

A --- one to many --- B ---one to many ---C (they are related by Set collection)

I have the outer join turned off and lazy = true at global level.

For specific instances however, I do need to load all the related collections B and C if I query for A.

The HQL query that I am using is:

session.find("from A as aa left outer join fetch aa.b as bb left join fetch bb.c where aa.id = 1);

I get the object A back. However, after closing the session, if I do a get size of any of the A's related collection, I get lazy initialization error.

"
Failed to lazily initialize a collection - no session or session was closed"


I was under the impression that doing a fetch should load the collection eagerly and can be accessed even if I close the session.

For the timebeing I don't have any records in B and C. However, I shoud have got size as 0 based on my understanding.

Am i misunderstanding something?


Thanks
Shishir


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 07, 2004 2:44 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
i think you can only load collection of B but not collection of C

Code:
select a from A as a left outer join fetch aa.b as bb where aa.id = 1

(with select a) this should return all a instances with its collections loaded, but for C ...

in the doc you have:
Quote:
Note that, in the current implementation, only one collection role may be fetched in a query


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 07, 2004 2:57 pm 
Regular
Regular

Joined: Fri Jan 16, 2004 4:48 am
Posts: 56
Crap:). You are right. It'd doing at only one level. What's the best way to implement thiis. My different business requirements require that sometime I traverse till B , or somethime I traverse all the way till C.

One approach is to have two methods in the business domain. One which just initializes the B . Other method doing Initialization of B as well as C for Each b. This then introduces duplication of code.

The other way is to load all the way till C in all cases. But then that's a permornce issue for cases C is being loaded when not required at all.

IMHO, I would go for duplication of code rather than the 2nd one. Any other suggestions ?

Thanks
Shishir


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 07, 2004 3:44 pm 
Regular
Regular

Joined: Fri Jan 16, 2004 4:48 am
Posts: 56
Also,

If I need to initialize B and C for A , how can I do it by join without iterating for each B obtained from A. In other words, how can I do it by breaking up the joins for A-B and B-C, note , I should get the same object A back and not another instance of A.

Thanks
Shishir


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 07, 2004 5:20 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
please reformulate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 07, 2004 7:25 pm 
Regular
Regular

Joined: Fri Jan 16, 2004 4:48 am
Posts: 56
What I meant was

How to initialize Collection B and collection C after quering for A (with global lazy = true and outer join = false, using explicit join statements,

without having to do a nested iteration through collection B and initializing C for each B.


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