首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QueryDsl投影将字段保留为空白

QueryDsl投影将字段保留为空白
EN

Stack Overflow用户
提问于 2020-12-22 15:43:33
回答 1查看 451关注 0票数 0
代码语言:javascript
复制
@Entity
public class DocumentConsolidated {
    @Id private UUID id;

    @OneToOne(fetch = FetchType.EAGER, optional = false, cascade = CascadeType.ALL)
    @JoinColumn(name = "metadata_id")
    private DocumentMetadata documentMetadata;

    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "documentConsolidated")
    private DocumentConfiguration documentConfiguration;
}

@Entity
public class DocumentConfiguration {
    @Id private UUID id;

    @OneToOne(fetch = FetchType.LAZY)
    @MapsId
    private DocumentConsolidated documentConsolidated;
}

// Service code:
QDocumentConsolidated qDoc = QDocumentConsolidated.documentConsolidated;
(JPQLQueryFactory) queryFactory
        .select(Projections.fields(qDoc, /*qDoc.id, */qDoc.documentConfiguration, qDoc.documentMetadata))
        .from(qDoc)
        .innerJoin(qDoc.documentConfiguration)
        .fetch();

这只有两种方式:

  • with qDoc.idid存在,documentConfiguration为空
  • 而没有qDoc.idE 210id为空,documentConfiguration存在于H 213F 214

为什么?

我已经检查过的是: Hibernate查询在Postgres客户机中运行时总是会引入documentConfiguration字段。在这两种情况下都存在Bot documentMetadata。

EN

回答 1

Stack Overflow用户

发布于 2020-12-29 17:28:44

问题没有得到解决,但我通过将Projections从混合中删除来解决这个问题:

代码语言:javascript
复制
// Service code:
QDocumentConsolidated qDoc = QDocumentConsolidated.documentConsolidated;
QDocumentConfiguration qCfg = qDoc.documentConfiguration;
QDocumentMetadata qMeta = qDoc.documentMetadata;

return queryFactory
        .select(qDoc, qCfg, qMeta) // <-- get rid of Projections
        .from(qDoc)
        .innerJoin(qCfg) // <-- manual join on Lazy entity (qMeta is auto-joined)
        .fetch().stream()
        .map(tuple -> { // <-- entity creation, similar with Projections.bean()
            DocumentConsolidated documentConsolidated = Objects.requireNonNull(tuple.get(qDoc));
            documentConsolidated.setDocumentMetadata(tuple.get(qMeta));
            documentConsolidated.setDocumentConfiguration(tuple.get(qCfg));
            return documentConsolidated;
        })
        .collect(Collectors.toList());
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65411633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档