【发布时间】:2020-06-22 10:43:13
【问题描述】:
我有以下查询,我在其中加入表 A、B 和 C:
-
C通过C.B_ID与B相关联 -
B通过B.A_ID与A相关联
我想检索一个报告,对于每个C,我还想从相应的B 和A 中检索字段。
如果只需要字段的子集,那么投影和获取到 POJO(具有来自 C、B、A 的所需属性)是一种显而易见的方法。
class CReportDTO {
Long c_id;
Long c_field1;
Long c_bid;
Long b_field1;
// ...
CReportDTO(Long c_id, Long c_field1, Long c_bid, Long b_field1) {
// ...
}
// ..
}
public List<CReportDTO> getPendingScheduledDeployments() {
return dslContext.select(
C.ID,
C.FIELD1,
C.B_ID,
B.FIELD1,
B.A_ID
A.FIELD1,
A.FIELD2
)
.from(C)
.join(B)
.on(C.B_ID.eq(B.ID))
.join(A)
.on(B.A_ID.eq(A.ID))
.fetchInto(CReportDTO.class);
};
}
我的问题
如果需要所有字段,我希望我的报告 DTO 包含 A、B、C POJO,而不会将它们展平:
class CReportDTO2 {
C c;
B b;
A a;
CReportDTO2(C c, B b, A a) {
// ...
}
// ..
}
是否可以将我的查询修改为:
- 包括每个表中的所有字段
- 将其按摩到
CReportDTO2,不要太冗长
【问题讨论】: