首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jOOQ:对于记录对象,带有fieldsRow()的fetchInto返回null,而对于POJO则不返回null

jOOQ:对于记录对象,带有fieldsRow()的fetchInto返回null,而对于POJO则不返回null
EN

Stack Overflow用户
提问于 2022-07-22 15:59:27
回答 1查看 138关注 0票数 1

最近我遇到了一些意想不到的jOOQ行为。我编写了一个查询,其中我将获取混合到jOOQ生成的POJO和jOOQ生成的记录类中。一位同事正确地指出,我应该保持一致,只使用记录或POJO类,所以我更新了查询以获取记录对象。

但是,我更新了fetchInto调用,使其指向一个记录类,而不是POJO,并且突然间,我以前工作的和其他没有变化的查询返回了所有字段都为null的记录对象!

最后,我尝试调整select查询,分别选择每个相关字段,然后正确地填充相关对象。

为什么会发生这种行为?

3种方案中的每一种方案的代码示例:

fieldsRow()和获取到生成的POJO中,一切都如预期的那样工作:

代码语言:javascript
复制
conf ->
    conf.dsl()
    .select(
        RUN.fieldsRow().as("run").convertFrom(r -> r.into(RunRecord.class)),
        RUN_PROGRESS
        .fieldsRow()
        .as("run_progress")
        .convertFrom(r -> r.into(RunProgressRecord.class)),
        multiset(
            select(REASON.fieldsRow()) <----- 
            .from(REASON)
            .where(REASON.RUN_ID.eq(RUN.ID)))
        .as("reasons")
        .convertFrom(r -> r.into(Reason.class)))   <---- 
    .from(RUN)
    .leftOuterJoin(RUN_PROGRESS)
    .on(RUN_PROGRESS.RUN_ID.eq(RUN.ID))
    .where(RUN.ID.eq(runId.getValue()))
    .fetch()

fieldsRow()并将其导入生成的记录类中,检索到的ReasonRecord对象的所有字段都为null!?!

代码语言:javascript
复制
conf ->
    conf.dsl()
    .select(
        RUN.fieldsRow().as("run").convertFrom(r -> r.into(RunRecord.class)),
        RUN_PROGRESS
        .fieldsRow()
        .as("run_progress")
        .convertFrom(r -> r.into(RunProgressRecord.class)),
        multiset(
            select(REASON.fieldsRow())
            .from(REASON)
            .where(REASON.RUN_ID.eq(RUN.ID)))
        .as("reasons")
        .convertFrom(r -> r.into(ReasonRecord.class)))   <---- 
    .from(RUN)
    .leftOuterJoin(RUN_PROGRESS)
    .on(RUN_PROGRESS.RUN_ID.eq(RUN.ID))
    .where(RUN.ID.eq(runId.getValue()))
    .fetch()

单独选择每个Reason字段,一切都按预期工作:

代码语言:javascript
复制
conf ->
    conf.dsl()
    .select(
        RUN.fieldsRow().as("run").convertFrom(r -> r.into(RunRecord.class)),
        RUN_PROGRESS
        .fieldsRow()
        .as("run_progress")
        .convertFrom(r -> r.into(RunProgressRecord.class)),
        multiset(
            select(REASON.RUN_ID, REASON.REASON_)  <------
            .from(REASON)
            .where(REASON.RUN_ID.eq(RUN.ID)))
        .as("reasons")
        .convertFrom(r -> r.into(ReasonRecord.class)))
    .from(RUN)
    .leftOuterJoin(RUN_PROGRESS)
    .on(RUN_PROGRESS.RUN_ID.eq(RUN.ID))
    .where(RUN.ID.eq(runId.getValue()))
    .fetch()
EN

回答 1

Stack Overflow用户

发布于 2022-07-23 10:10:39

一个更简单的解决方案

假设ReasonRecord是为REASON表生成的TableRecord,然后从jOOQ 3.17开始,您可以简单地投影表引用本身:

代码语言:javascript
复制
select(REASON)
.from(REASON)
.where(REASON.RUN_ID.eq(RUN.ID))

这样,您就不必再应用任何临时转换来获得嵌套的Result<ReasonRecord>了。(您仍然可以保留它,以便将Result<ReasonRecord>转换为List<ReasonRecord>,如果您的输入有此必要.)。

同样,您不需要再使用RUN.fieldsRow()了。只是项目RUN

代码语言:javascript
复制
conf.dsl()
    .select(
       RUN,
       RUN_PROGRESS,
       ...)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73083090

复制
相关文章

相似问题

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