let’s say that i have entity(root entity) that has nested 1:N entities
public class RootEntity{
private String name;
private List<ChildEntity> child;
}
public class ChildEntity{
private String cname;
private List<CChildEntity> cchild;
}
public class CChildEntity{
private String ccname;
}
public record RootDto(
private String name,
private List<ChildDto> child
){}
public record ChildDto(
private String cname,
private List<CChildDto> cchild
){}
public record CChildDto(
private String ccname
){}
and trying to retrieve all with QueryDSL
@Transactional
public List<RootDto> get(){
JPAQuery<RootDto> query = queryFactory
.selectDistinct(Projections.constructor(ResponseDto.class, RootEntity))
.from(root)
.leftJoin(root.childs, child)
.leftJoin(child.cchilds, cchild)
List<RootDto> content = query.fetch()
return content;
}
Above execution causes N+1 problem. which i did not expected/
But below does not have N+1 problem. why is that?
Those two are looks like same operation to me.
@Transactional
public List<RootDto> get(){
JPAQuery<RootDto> query = queryFactory
.selectDistinct(root)
.from(root)
.leftJoin(root.childs, child)
.leftJoin(child.cchilds, cchild)
List<RootDto> content = query.fetch().stream().map(RootDto::new).toList();
return content;
}
and if Projections.contsructor
does not support EntityManager
to initialize those entity via Dtos, why is that? and how could that be possible? (again, looks similar to me)
uicheon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.