在加入 Hibernate 中获取关联

Fetching associations in joins Hibernate

我是 Hibernate 的新手,在通过连接中的获取关联时,我遇到了以下查询:

em.createQuery(select guide from Guide guide join fetch guide.Students students);

sql 中的上述查询看起来像:

select 
guide0_id as id_1_2_0,
students1_id as id_1_6_1,
guide0_name as name_2_2_0,
students1_guide_id as guide_id_4_6_1....

Student 和 guide 是多对一的关系,Student 是关系的所有者。上面的查询也会获取学生的详细信息,即使默认情况下指南是延迟加载的。 我的问题是,如果我们的目的是让学生在 select 子句中以及在上面的 SQL 中看到,为什么我们不能做一个简单的:

em.createQuery(select guide,students from Guide guide join guide.Students 
students);

通过在 select 子句中添加 students 列?

// you can use simple join query to fetch your desired record as you want
// and also you can get by Hibernate Criteria instead of using the SQL Query like 
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="child_table_column",referencedColumnName="id")
private ParentTable updatedBy;
// use above for POJO Configuration and then use simple criteria to get the DATa

The above query will fetch students details as well even though guide is lazily loaded by default

因为你使用了 'join fetch' 而不是 'join',你让学生们迫不及待地加载。