简体   繁体   English

使用带有部分数据的 POJO 仅更新 JOOQ 记录中的更改字段

[英]Update only changed fields in JOOQ record using POJO with partial data

I have a very similar issue as the following post, but slight variation.我有一个与以下帖子非常相似的问题,但略有不同。 Update only changed fields in JOOQ record using POJO 使用 POJO 仅更新 JOOQ 记录中更改的字段

I get the same JSON object both for create and update call.我得到相同的 JSON object 用于创建和更新调用。 In the update call, I might not receive all the fields, so I want to make sure I update only the changed fields and not replace the fields that weren't provided with null.在更新调用中,我可能不会收到所有字段,因此我想确保仅更新更改的字段,而不是替换 null 未提供的字段。 There is no constraint on the database.对数据库没有约束。

I am following the above answer, but running into an error with slight modification.我遵循上述答案,但稍作修改时遇到错误。

UserRecord existingRecord = existingUserRecordOptional.get();
UserRecord newUserRecord = new UserRecord();
newUserRecord.from(userPojo);

for (int i = 0; i < newRecord.size(); i++) {
   if (nonNull(newRecord.get(i)) && !Objects.equals(existingConsumerRecord.get(i), newRecord.get(i))) {
      existingUserRecord.setValue(DSL.val(existingUserRecord.field(i)), DSL.val(newUserRecord.getValue(i)));
   }
}

Error I am receiving is:我收到的错误是:

Field ('"db"."table"."description"') is not contained in Row (...)字段 ('"db"."table"."description"') 不包含在 Row (...)

Do this instead:改为这样做:

existingUserRecord.setValue(
  // Don't wrap this in DSL.val()
  (Field) existingUserRecord.field(i), 
  DSL.val(newUserRecord.getValue(i))
);

DSL.val() is used to create a bind value. DSL.val()用于创建绑定值。

However, since you've linked to the previous question , why not just follow that approach that doesn't copy values between records?但是,既然您已链接到上一个问题,为什么不遵循不复制记录之间的值的方法呢? You could use the newUserRecord and unset all changed flags for the columns you don't want to send to the database.您可以使用newUserRecord并取消设置您不想发送到数据库的列的所有更改标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM