-->
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: Initializing a many-to-many collection
PostPosted: Mon Oct 20, 2003 12:00 pm 
Beginner
Beginner

Joined: Mon Sep 29, 2003 3:10 pm
Posts: 36
I have a unidirectional many-to-many relationship between QuestionGroup and Question where QuestionGroup has a method, "getQuestions()".

I use Session.load() to load an instance of QuestionGroup and then immediately follow that load with a Hibernate.initialize(questionGroup.getQuestions()) to load the Questions (which are mapped with lazy="true"
Code:
QuestionGroupDO qdo = (QuestionGroupDO)session.load(QuestionGroupDO.class, id);
Hibernate.initialize(qdo.getQuestions());

After this I commit the transaction and close the session. What I would expect is that I should now be able to access the Questions in this QuestionGroup without getting a Lazy Initialization Exception. However, this isn't the case. The only value being populated in the Question objects is the QuestionID. When I look at the querys being run, they show the same thing. A full select is run to populate the QuestionGroup and then, when the Questions collection is initialized, a select is being run against the associative table (questiongroup_question) to retrieve the QuestionID but the select doesn't join the question table in order to get the rest of the data needed to populate each Question object in the collection.

What's going on here?

-Matt


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 20, 2003 1:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
So where is your mapping? Did you specify outer-join="true" on the <many-to-many> element?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 20, 2003 3:02 pm 
Beginner
Beginner

Joined: Mon Sep 29, 2003 3:10 pm
Posts: 36
Ok, I'm completely confused. A few days ago, I spent a load of time going back and forth with Steve and another Hibernate user about what the outer-join attribute was and under what situations it was used and I thought I finally understood

Doesn't the fact that I'm manually initializing the collection with Hibernate.initialize() completely bypass anything to do with outer-join="true"? If not, then what's the purpose of this the Hibernate.initialize() method anyway?

Basically, 80% of the time that I retrieve a QuestionGroup, I don't need the child Questions and so retieving the Questions every time would be terribly inefficient. Nevertheless, that other 20% of the time, when I do need the Questions, I though that I could use the Hibernate.initialize() method to have them be populated.

I guess I can iterate through the Questions while the session is still open and arbitrarily retrieve one of the properties to insure that the object is populated, but I thought that the Hibernate.initialize() method was created to prevent just that kind of unintuitive code pattern.

-Matt


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 23, 2003 1:30 pm 
Beginner
Beginner

Joined: Mon Sep 29, 2003 3:10 pm
Posts: 36
Sorry to bring this back up again, but I'm still having trouble understanding how and when "outer-join" should be used. In the documentation, when I do a search for "outer-join", I find the following statement in section 2.5.2:

Quote:
Outer join fetching allows a graph of objects connected by many-to-one or one-to-one associations to be retrieved in a single select. The fetched graph ends at leaf objects, objects with proxies or where circular references occur. This behaviour may be disabled for a particular association by setting outer-join="false" in the XML mapping metadata.

If I continue to search the documentation, the only remaining places where outer-join is mentioned is in reference to many-to-one relationships (4.1.10) and one-to-one relationships (4.1.11). I'm unable to find in the documentation how outer-join should be used in many-to-many situations like the one I described in my initial post.

Is there some supplemental documentation which better describes this?

-Matt


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 23, 2003 1:51 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You are confused because there are TWO levels here:

(1) the collection (which you are initializing by calling Hibernate.initialize())
(2) the collection elements

Either may be loaded lazily in the case of a many-to-many. Makes sense - there are two diff tables!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 23, 2003 2:58 pm 
Beginner
Beginner

Joined: Mon Sep 29, 2003 3:10 pm
Posts: 36
Thanks, Gavin, that does (kind of) make sense, especially if one is looking at the relationship from the perspective of the database. I suppose that I was just assuming that the relationship would be viewed from the perspective of the java objects instead.

I guess that way I was looking at it, a one-to-many and a many-to-many are not very different fromt he prespective of my Java objects. Both are about one entity containing a collection of another.

In any case, I think I understand how to map my objects now to achieve the results I'm looking for. Many-to-many relationships need to have their outer-join attribute set to "true" if I want Hibernate.initialize(children) to properly retrieve the child objects and one-to-many relationships don't need outer-join set to "true" in order to achieve the same result.

Thanks,
Matt[/i]


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.