使用 POJO 仅更新 JOOQ 记录中的更改字段
Update only changed fields in JOOQ record using POJO
我想使用 POJO 作为源来更新 JOOQ 记录中的更改字段。 Record.from(Object)
几乎是正确的,但根据文档
The resulting record will have its internal "changed" flags set to true for all values.
我只希望实际更改的字段(由 Objects.equals(Object, Object)
确定)更新其标志。
这两个原因是:
- 我不想触发插入
- 我只想在更新语句中向数据库发送新值(带宽、并发更新等)
此实施的原因...
The resulting record will have its internal "changed" flags set to true for all values.
... 很简单:如果事情不是以这种方式实现的,就没有办法强制更新未更改的值。在某些用例中这是可取的(例如批处理,避免太多不同的 SQL 字符串等)。 Record.from()
method is just consistent with the other Record
methods, e.g. Record.set(Field, Object)
.
您可以像这样修补内部更改的标志:
// Load all values and mark them all as "changed"
record.from(object);
// Undo the undesired flags
for (int i = 0; i < record.size(); i++)
if (Objects.equals(record.get(i), record.original(i)))
record.changed(i, false);
我还在 jOOQ 中创建了一个功能请求。也许 API 可以改进,因为很多人都有这个需求:
https://github.com/jOOQ/jOOQ/issues/5394
我想使用 POJO 作为源来更新 JOOQ 记录中的更改字段。 Record.from(Object)
几乎是正确的,但根据文档
The resulting record will have its internal "changed" flags set to true for all values.
我只希望实际更改的字段(由 Objects.equals(Object, Object)
确定)更新其标志。
这两个原因是:
- 我不想触发插入
- 我只想在更新语句中向数据库发送新值(带宽、并发更新等)
此实施的原因...
The resulting record will have its internal "changed" flags set to true for all values.
... 很简单:如果事情不是以这种方式实现的,就没有办法强制更新未更改的值。在某些用例中这是可取的(例如批处理,避免太多不同的 SQL 字符串等)。 Record.from()
method is just consistent with the other Record
methods, e.g. Record.set(Field, Object)
.
您可以像这样修补内部更改的标志:
// Load all values and mark them all as "changed"
record.from(object);
// Undo the undesired flags
for (int i = 0; i < record.size(); i++)
if (Objects.equals(record.get(i), record.original(i)))
record.changed(i, false);
我还在 jOOQ 中创建了一个功能请求。也许 API 可以改进,因为很多人都有这个需求: https://github.com/jOOQ/jOOQ/issues/5394