I have a following query where I join tables A
,B
, and C
:
C
is related toB
viaC.B_ID
B
is related toA
viaB.A_ID
I want to retrieve a report, where for each C
, I want to retrieve also fields from corresponding B
and A
.
If only a subset of fields is required, a projection and fetching to a POJO (with required properties from C
, B
, A
) is an obvious approach.
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);
};
}
My question
In case where all fields are needed I would prefer to have my report DTO contain A
, B
, C
POJOs, without flattening them out:
class CReportDTO2 {
C c;
B b;
A a;
CReportDTO2(C c, B b, A a) {
// ...
}
// ..
}
Is it possible to modify my query to:
- include all fields from each table
- massage it into
CReportDTO2
without too much verbosity