繁体   English   中英

QueryDSL fetchJoin 不获取数据

[英]QueryDSL fetchJoin does not fetch the data

我正面临这个问题:

假设我有 3 个这样的实体:

实体 A:

long id

String someField

// No bidirectional linkage to B entity via hibernate

实体 B:

long id

String someBField

@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name="b_id")
A entityA

@ManyToOne(optional = true, fetch = FetchType.LAZY)
@JoinColumn(name="b_id")
C entityC;   

实体 C:

long id

String someCField

// No bidirectional linkage to B entity via hibernate

现在,目标是(为简单起见,有一些排序和过滤,但不影响我的问题)返回所有 B 记录,每一条都带有 A 和 C 记录

所以我正在做这样的事情(我习惯于使用 spring-data-jpa 来(左)JOIN FETCH 属性以避免按需延迟加载以防止将无用的查询触发到数据库中,我想在 QueryDSL 中做完全相同的事情)

    JPAQuery<DealBo> query = new JPAQuery<>(entityManager);

    query.select(qB)
            .from(qB)
            .innerJoin(qA).on(qA.a_id.eq(qB.id)).fetchJoin()
            .innerJoin(qC).on(qC.a_id.eq(qB.id)).fetchJoin()
            .fetch()

And I expect one SQL where in select clause there are data from all 3 tables (entities), where QueryDSL (or Hibernate, I am not completely sure what tool will do SQL -> Entity mapping) map the result to Entity objects. 但我真正得到的只是 select 之类的

select b.id, b.someBfield from b
inner join a // join clause is right and omitted for simplicity
inner join b // join clause is right and omitted for simplicity

因此,当我调用一项时,QueryDSL 例如返回了什么

b.getC() 或 b.getA(),我正在向数据库发起另一个查询,这是我首先要避免的事情。

我究竟做错了什么?

我认为,连接条件的定义是不合适的。

希望我已经用 UserEntity <- UserRoleEntity -> RoleEntity 重新创建了描述的星座:

@Entity
@Table(name = "t_user")
public class UserEntity {

    @Id
    @Column(name = "id")
    private Integer id;
    @Column(name = "name") 

    // ..
}

@Entity
@Table(name = "t_user_role")
public class UserRoleEntity {
    @Id
    @Column(name = "id")
    private Integer id;
    @ManyToOne
    @JoinColumn(name = "user_id")
    private UserEntity user;
    @ManyToOne
    @JoinColumn(name = "role_id")
    private RoleEntity role;

    // ..
}

@Entity
@Table(name = "t_role")
public class RoleEntity {
    @Id
    @Column(name = "id")
    private Integer id;
    @Column(name = "name")
    private String name;

    // ..
}

查询

List<UserRoleEntity> findAll() {
    JPAQuery<UserRoleEntity> query = new JPAQuery<>(entityManager);

    return query.select(QUserRoleEntity.userRoleEntity)
            .from(QUserRoleEntity.userRoleEntity)
            .innerJoin(QUserRoleEntity.userRoleEntity.user).fetchJoin()
            .innerJoin(QUserRoleEntity.userRoleEntity.role).fetchJoin()
            .fetch();

}

获取关联的表,并且对用户关联的后续迭代不会从数据库加载用户实体。

生成的 SQL 看起来像

    select 
        userroleen0_.id as id1_5_0_, 
        userentity1_.id as id1_4_1_, 
        roleentity2_.id as id1_2_2_, 
        userroleen0_.role_id as role_id2_5_0_, 
        userroleen0_.user_id as user_id3_5_0_, 
        userentity1_.name as name2_4_1_, 
        roleentity2_.name as name2_2_2_ 
    from t_user_role userroleen0_ 
    inner join t_user userentity1_ on userroleen0_.user_id=userentity1_.id 
    inner join t_role roleentity2_ on userroleen0_.role_id=roleentity2_.id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM