I have the following class setup for a Student in Hibernate. Class student contains a set of Score objects. Each score object contains the score, student id, and a gradeEvent object. A grade event object contains things such as date, description, etc. I want to fetch a student and load all there associated score objects and the gradeEvent object associated with each Score object that has been fetched. The following HQL lets me access a Student object with the fields of the associated Score objects accept for gradeEvent
"from Student student left join fetch student.scores where student.studentId=1"
How can I load the gradeEvent object at query time so I can access it after the session's been closed? I've put the relevant sections of my mapping files below.
student
<set name="scores" inverse="true" lazy="true" table="score" fetch="select">
<key>
<column name="student_id" not-null开发者_JS百科="true" />
</key>
<one-to-many class="gradebook.model.Score" />
</set>
score
<many-to-one name="student" class="gradebook.model.Student" update="false" insert="false" fetch="select">
<column name="student_id" not-null="true" />
</many-to-one>
<many-to-one name="gradeEvent" class="gradebook.model.GradeEvent" update="false" insert="false" fetch="select">
<column name="event_id" not-null="true" />
</many-to-one>
gradeEvent
<set name="scores" inverse="false" lazy="true" table="score" fetch="select">
<key>
<column name="event_id" not-null="true" />
</key>
<one-to-many class="gradebook.model.Score" />
</set>
You can access it exactly the same way you did with scores; you just need to assign an alias to collection so you can refer to it again:
from Student student
left join fetch student.scores score
left join fetch score.gradeEvent
where student.studentId=1
精彩评论