I am facing this issue:
Lets assume I have 3 entities like this:
Entity A:
long id
String someField
// No bidirectional linkage to B entity via hibernate
Entity 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;
Entity C:
long id
String someCField
// No bidirectional linkage to B entity via hibernate
Now, the goal is to (for simplicity, there are some ordering and filtering but that does not affect my question) return all B records, every one with A and C record fetched
So I am doing something like this (I am used to use spring-data-jpa to (LEFT) JOIN FETCH the properties to avoid lazy loading on demand to prevent firing useless queries into database and I want to do exactly same thing in 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. But what I am really getting is just select like
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
So when I call on one item what QueryDSL returned for example
b.getC() or b.getA(), I am firing another queries into database, what is a thing I want to avoid in first place.
What am I doing wrong?