QueryDSL - 一对多关系的 DTO 投影

use*_*119 5 java dsl jpa dto querydsl

我正在考虑使用 DTO 投影 - 我有两个具有一对多关系的实体(EntityOne 的一个实例链接到 EntityTwo 的多个实例),并且我想将结果作为新的 DTO 对象返回 - 我的内容我目前正在尝试的是这样的:

query.select(Projections.constructor(MyDtoObject.class,
            entityOne, list(entityTwo)))
        .from(entityOne, entityTwo)
        .where(......)
Run Code Online (Sandbox Code Playgroud)

MyDtoObject 看起来像这样:

public class MyDtoObject {

    private EntityOne entityOne;
    private Collection<EntityTwo> entityTwoCollection 

   // getters, setters and an all args constructor method here

}
Run Code Online (Sandbox Code Playgroud)

然而,这会带回比预期多得多的 MyDtoObjects,而且看起来每个 MyDtoObjects 只保存一个entityTwo 对象而不是一个集合。

如何指示 queryDSL 创建具有多个entityTwo 条目的 MyDtoObjects 结果对象?list(..) 方法在我上面的场景中是否执行任何操作?

小智 3

queryFactory.from(Entity)
    .transform(groupBy(Entity.something).list((    
        Projections.fields(EntityDto.class,
            list(Projections.fields(Dto.class, 
                )).as("CollectionEntity"),
    )));
Run Code Online (Sandbox Code Playgroud)

尝试用列表进行转换