-->
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: Does Hibernate.initialize() abide by outer-join?
PostPosted: Mon Nov 10, 2003 9:33 pm 
Beginner
Beginner

Joined: Mon Sep 29, 2003 3:10 pm
Posts: 36
Does Hibernate.initialize abide by a mapping being set to outer-join="true" when the object in question wasn't explicitly loaded with Session.load() but is, instead, a child of an object that was loaded with Session.load()?

An example: Let's assume I have a User object that contains a subordinate object (i.e. Department) mapped as a many-to-one with outer-join="true" like this:
Code:
<hibernate-mapping>
    <class
        name="eg.User"
        table="users"
        proxy="eg.User">

        <id name="assessmentID"
            column="assessment_id"
            type="java.lang.Long"
            unsaved-value="null" >
            <generator class="native" />
        </id>

        <many-to-one
            name="department"
            class="eg.Department"
            outer-join="true"
            column="department_id"
        />
</hibernate-mapping>


Ok, I know that if I were to do the following, the Department would be retrieved automatically:
Code:
User user = (User)session.load(User.class, id);
Hibernate.initialize(user);


My question is if I were to have a List of Users that was a part of another object and then manually iterate through the list and perform a Hibernate.initialize(user) on each User in the list, would the Department be automatically loaded for each User? I assumed it would be, however that's not the results I'm getting.

My specific example is this. I have a relationship in my system between Users and Assessments (this is a commercial LMS system). The relationship is called UserAssessment (yes, I know. I'm awfully creative.)

Each Assessment contains a List of QuestionGroup objects, which each in turn contains a list of Question objects. I'm loading the UserAssessment with a Session.load() call and the initializing the QuestionGroups like this:
Code:
UserAssessmentDO userAssessment = (UserAssessmentDO)session.load(returnDO, userAssessmentID);

List questionGroups = userAssessment.getAssessment().getQuestionGroups();
for (int i = 0; i < questionGroups.size(); i++) {
    QuestionGroupDO questionGroup = (GroupDO)questionGroups.get(i);
    Hibernate.initialize(questionGroup.getQuestionGroup());
    Hibernate.initialize(questionGroup.getQuestionGroup().getQuestions());
}

The QuestionGroups (and Assessments, for that matter) contain a subordinate object called Topic mapped with a many-to-one in exactly the same way as I described the Department in my example above, however, the Topic object is NOT being automatically retrieved when I initialize the QuestionGroups.

I realize, of course, that I could manually Hibernate.initialize() each Topic object, but this seems inefficient as I would be using a separate database hit to retrieve the Topic when it should be retrieved in the same SQL statement as the QuestionGroup object itself.

Am I doing something wrong here?

-Matt


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2003 9:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
Does Hibernate.initialize abide by a mapping being set to outer-join="true" when the object in question wasn't explicitly loaded with Session.load() but is, instead, a child of an object that was loaded with Session.load()?


Yes.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 10:30 am 
Beginner
Beginner

Joined: Mon Sep 29, 2003 3:10 pm
Posts: 36
Ok, thank you for the reply. Given my example with Users and Assessments from above, is there any reason anyone can see why it is not happening?

A bit of additional information:
Upon further review of the SQL being run, I can see that the Topic object is being properly retrieved, however Hibernate isn't populating the QuestionGroup with that subordinate object. It is using a proxy instead even though Topic is mapped within QuestionGroup with outer-join="true".

I would be glad top post any additional mappings or code if it would be helpful.

-Matt


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 10:31 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Is there an uninitialized proxy for that object ALREADY associated with the session.


(Answer is "yes", I would expect.)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 11:24 am 
Beginner
Beginner

Joined: Mon Sep 29, 2003 3:10 pm
Posts: 36
An answer within one minute. That's got to be some kind of record. ;)

Topic is a subordinate to a large number of objects within our system (Courses, Assessments, Lessons, etc. all have Topics, naturally) so it's certainly possible that I'm loading an object containing a Topic before this and that it isn't initialized. However, why would this be affecting an object where I am initializing it?Ok, let me think this through....

A Topic object with an ID of 12 is loaded into the Session, but not initialized. Later on, a second object requests that same Topic object. The second request DOES run the SQL to retrieve that object, but it doesn't actually load the information into the object.

There are two things I'm not understanding:
1) It makes sense that Hibernate would load the object from the session if it already exists there rather than try to reload it from the database, however, if the instance that is loaded into the session is an empty proxy, then it would seem that Hibernate is NOT abiding by the outer-join="true" mapping for the QuestionGroup object when it chooses to use that empty proxy rather than loading the information from the database and using it instead. Furthermore, Hibernate actually IS running the SQL to load the information from the database but still using the proxided object from the session instead of the information just retrieved from the database. So basically I'm getting the worst of both worlds. I'm sufferring the database traffic for loading the information, but not actually having access to the information in my object.

2) I'd like for the actions I take to produce consistent behavior. If in one place in my code I do a Hibernate.initialize(QuestionGroup) and get one result and then in a different place in my code I perform the same action again on the exact same object but get a different result, I'm going to be in trouble. It seems like I should be able to expect the same same result from those two actions.

Ok, so you expected the answer to your question is "yes". Let's assume you're correct. What does that mean? Is there a way to deal with this situation?

By the way, thank you for your responses so far. I may seem a little confrontational, but I truly appreciate the help.

-Matt


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 11:33 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
What you need to understand is the difference between an object being loaded and a pre-existing proxy being initialized. They are different. The proxy could be uninitialized, even if the object is loaded.

Access the proxy, see if it causes another database hit. It shouldn't, if my theory is correct.


In general, I'm not sure that I can make any guarantees like: "when an object is loaded, all uninitialized proxies of it are connected to it." But perhaps some improvements could be made.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 11:58 am 
Beginner
Beginner

Joined: Mon Sep 29, 2003 3:10 pm
Posts: 36
gavin wrote:
Access the proxy, see if it causes another database hit. It shouldn't, if my theory is correct.

Indeed, you are correct. Oh well. At least now I understand what's going on and can better adjust my code to work with it.

I honestly don't know where you guys find the time to answer so many questions on the forums and also continue such fast paced work on product development. In any case, thanks. Seriously.

-Matt


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 12:00 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Cheers :)


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.