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