【问题标题】:QueryDSL - DTO projections for one-to-many relationshipQueryDSL - 一对多关系的 DTO 预测
【发布时间】:2019-03-07 10:45:40
【问题描述】:

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

query.select(Projections.constructor(MyDtoObject.class,
            entityOne, list(entityTwo)))
        .from(entityOne, entityTwo)
        .where(......)

MyDtoObject 如下所示:

public class MyDtoObject {

    private EntityOne entityOne;
    private Collection<EntityTwo> entityTwoCollection 

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

}

但是,这带来的 MyDtoObjects 比预期的要多得多,而且看起来每个人只持有一个 entityTwo 对象而不是一个集合。

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

【问题讨论】:

标签: java jpa dsl querydsl dto


【解决方案1】:
queryFactory.from(Entity)
    .transform(groupBy(Entity.something).list((    
        Projections.fields(EntityDto.class,
            list(Projections.fields(Dto.class, 
                )).as("CollectionEntity"),
    )));

尝试使用列表进行转换

【讨论】:

    猜你喜欢
    • 2021-10-18
    • 1970-01-01
    • 2016-08-03
    • 2019-01-28
    • 2021-07-18
    • 2017-10-19
    • 2017-07-18
    • 1970-01-01
    • 2017-12-03
    相关资源
    最近更新 更多