简体   繁体   English

jOOQ-基于唯一键(不是主键)更新记录

[英]jOOQ - update record based on unique key (not primary key)

I am using jOOQ to generate POJOs for my database tables. 我正在使用jOOQ为我的数据库表生成POJO。 This works great. 这很好。

I have a table with a primary key ( identifier ) and a unique key ( name ). 我有一个带有主键( identifier )和唯一键( name )的表。 When updating the record, jOOQ uses the primary key. 更新记录时,jOOQ使用主键。

I would like to update the record by using the unique key instead of the primary key. 我想通过使用唯一键而不是主键来更新记录。

https://github.com/jOOQ/jOOQ/blob/master/jOOQ/src/main/java/org/jooq/impl/UpdatableRecordImpl.java https://github.com/jOOQ/jOOQ/blob/master/jOOQ/src/main/java/org/jooq/impl/UpdatableRecordImpl.java

@Override
public final int update() {
    return update(fields.fields.fields);
}

@Override
public int update(Field<?>... storeFields) throws DataAccessException, DataChangedException {
    return storeUpdate(storeFields, getPrimaryKey().getFieldsArray());
}

In essence, I want to call storeUpdate with another key (second parameter). 本质上,我想使用另一个键(第二个参数)调用storeUpdate I tried extending the generated record, but storeUpdate is private. 我尝试扩展生成的记录,但storeUpdate是私有的。

Is there another way to update a record? 还有另一种更新记录的方法吗? I could first select the identifier before update() , but it introduces an extra query, which I would like to avoid. 我可以先在update()之前选择标识符,但是它引入了一个额外的查询,我想避免这样做。

From the comments, I understand that you want to: 从这些评论中,我了解到您想要:

  • Use the generated records as "ActiveRecords" holding data that is going to be stored / updated into a table 将生成的记录用作“ ActiveRecords”,其中包含将要存储/更新到表中的数据
  • Use arbitrary "key" information as selective criteria for your update statement 使用任意的“关键”信息作为更新语句的选择标准

There are two ways you can do this with jOOQ: 您可以通过两种方式使用jOOQ:

1. Override the primary key information in the code generator 1.覆盖代码生成器中的主键信息

You can specify a regular expression matching unique key names in your database, which should override primary keys in generated code: 您可以在数据库中指定与唯一键名称匹配的正则表达式,该名称应覆盖生成的代码中的主键:

  <!-- All (UNIQUE) key names that should be used instead of primary keys on
       generated UpdatableRecords, to be used with

        - UpdatableRecord.store()
        - UpdatableRecord.update()
        - UpdatableRecord.delete()
        - UpdatableRecord.refresh()

        If several keys match, a warning is emitted and the first one encountered 
        will be used.

        This flag will also replace synthetic primary keys, if it matches. -->
  <overridePrimaryKeys>MY_UNIQUE_KEY_NAME</overridePrimaryKeys>

Note that this solution will affect all the calls to store() , update() , etc. From your comments, this might not be the desired behaviour... For more information, see the jOOQ manual 请注意,此解决方案将影响对store()update()等的所有调用。根据您的评论,这可能不是您想要的行为。 有关更多信息,请参见jOOQ手册。

2. Use a regular UPDATE statement 2.使用常规的UPDATE语句

You can pass the whole UpdatableRecord to an UPDATE statement and specify the selection criteria explicitly, such as: 您可以将整个UpdatableRecord传递给UPDATE语句,并显式指定选择条件,例如:

MyTableRecord record = ...;
DSL.using(configuration)
   .update(MY_TABLE)
   .set(record)
   .where(MY_TABLE.NAME.eq(record.getName())
   .execute();

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

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