最近我遇到了一些意想不到的jOOQ行为。我编写了一个查询,其中我将获取混合到jOOQ生成的POJO和jOOQ生成的记录类中。一位同事正确地指出,我应该保持一致,只使用记录或POJO类,所以我更新了查询以获取记录对象。
但是,我更新了fetchInto调用,使其指向一个记录类,而不是POJO,并且突然间,我以前工作的和其他没有变化的查询返回了所有字段都为null的记录对象!
最后,我尝试调整select查询,分别选择每个相关字段,然后正确地填充相关对象。
为什么会发生这种行为?
3种方案中的每一种方案的代码示例:
将fieldsRow()和获取到生成的POJO中,一切都如预期的那样工作:
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!?!
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字段,一切都按预期工作:
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()发布于 2022-07-23 10:10:39
一个更简单的解决方案
假设ReasonRecord是为REASON表生成的TableRecord,然后从jOOQ 3.17开始,您可以简单地投影表引用本身:
select(REASON)
.from(REASON)
.where(REASON.RUN_ID.eq(RUN.ID))这样,您就不必再应用任何临时转换来获得嵌套的Result<ReasonRecord>了。(您仍然可以保留它,以便将Result<ReasonRecord>转换为List<ReasonRecord>,如果您的输入有此必要.)。
同样,您不需要再使用RUN.fieldsRow()了。只是项目RUN
conf.dsl()
.select(
RUN,
RUN_PROGRESS,
...)https://stackoverflow.com/questions/73083090
复制相似问题