【问题标题】:Retrieving and loading third level referenced classes through HQL通过 HQL 检索和加载第三级引用的类
【发布时间】:2009-11-02 23:16:00
【问题描述】:

我在 Hibernate 中为学生设置了以下课程。 学生类包含一组分数对象。每个分数对象都包含分数、学生 ID 和一个 GradeEvent 对象。 成绩事件对象包含日期、描述等内容。 我想获取一个学生并加载所有关联的分数对象以及与已获取的每个分数对象关联的 GradeEvent 对象。下面的 HQL 让我可以访问一个 Student 对象,该对象的字段为 GradeEvent 接受的关联 Score 对象

"from Student student left join fetch student.scores where student.studentId=1"

如何在查询时加载gradeEvent 对象,以便在会话关闭后访问它?我已将映射文件的相关部分放在下面。

学生

<set name="scores" inverse="true" lazy="true" table="score" fetch="select">
<key>
<column name="student_id" not-null="true" />
</key>
<one-to-many class="gradebook.model.Score" />
</set>

分数

<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>

等级事件

<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>

【问题讨论】:

    标签: hibernate hql


    【解决方案1】:

    您可以像访问乐谱一样访问它;您只需为集合分配一个别名,以便再次引用它:

    from Student student
     left join fetch student.scores score
     left join fetch score.gradeEvent
    where student.studentId=1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-22
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 2016-09-17
      • 1970-01-01
      • 2015-04-11
      • 2014-09-12
      相关资源
      最近更新 更多