如何在 jOOQ 中配置乐观锁定?
How to configure optimistic locking in jOOQ?
从 jOOQ Manual 开始,至少我不清楚如何正确配置乐观锁定。
<!-- All table and view columns that are used as "version" fields for
optimistic locking (A Java regular expression. Use the pipe to separate several expressions).
See UpdatableRecord.store() and UpdatableRecord.delete() for details -->
<recordVersionFields>REC_VERSION</recordVersionFields>
及以下
recordVersionFields: Relevant methods from super classes are overridden to return the VERSION field
这到底是什么意思?有人可以举个例子吗?
想象一下,如果有这个table,例如:
CREATE TABLE "users" (
"id" INTEGER DEFAULT nextval('global_seq') NOT NULL,
"code" TEXT NOT NULL,
"version" INTEGER NOT NULL
);
我应该把什么放到 recordVersionFields
到 pom.xml 中?
<database>
<inputSchema>PUBLIC</inputSchema>
<recordVersionFields>users.version</recordVersionFields>
</database>
users.version
, version
, RECORD_VERSION
?
我用的是H2数据库,如果只设置version
会出现编译错误
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /D:/JooqDemo/target/generated-sources/jooq-h2/org/jooqdemo/generated/tables/SchemaVersion.java:[152,52] getRecordVersion() in org.jooqdemo.generated.tables.SchemaVersion cannot implement getRecordVersion() in org.jooq.Table
return type org.jooq.TableField<org.jooqdemo.generated.tables.records.SchemaVersionRecord,java.lang.String> is not compatible with org.jooq.TableField<org.jooqdemo.generated.tables.records.SchemaVersionRecord,? extends java.lang.Number>
您可以提供给代码生成器的所有这些正则表达式模式都是匹配模式、tables 或列的模式,它们的 unqualified 名称 (例如 version
) 或他们的 完全限定 名称(例如 public.users.version
)。您不能使用部分限定的名称,例如 users.version
.
为了回答您的具体问题,以下是可行的示例:
<!-- Will match version columns in all tables -->
<recordVersionFields>version</recordVersionFields>
<!-- Will match version columns in all user tables.
Notice that schema names are matched by .* -->
<recordVersionFields>.*\.user\.version</recordVersionFields>
<!-- Will match version columns only in that particular public.user table -->
<recordVersionFields>public\.user\.version</recordVersionFields>
这会将相关元信息生成到您的 UserRecord
中,以便在 UserRecord.store()
、update()
和 delete()
调用中启用乐观锁定。
旁注:您的编译错误
在你的问题中,你提到了一个似乎发生的编译错误,因为你正在匹配所有 table 中的所有版本列:
<!-- Will match version columns in all tables -->
<recordVersionFields>version</recordVersionFields>
这也将匹配 "wrong" 版本列,例如在 schema_version
table 中,它的类型是 VARCHAR
而不是 INTEGER
。也许,您应该更明确地使用正则表达式来进行精确匹配。
旁注:区分大小写
所有这些正则表达式都区分大小写。如果您使用的是 PostgreSQL,则以下内容可能是错误的:
<inputSchema>PUBLIC</inputSchema>
相反,写下这些:
<!-- use lower-case in PostgreSQL by default -->
<inputSchema>public</inputSchema>
<!-- use case-insensitive regular expressions to "stay safe" -->
<inputSchema>(?i:PUBLIC)</inputSchema>
从 jOOQ Manual 开始,至少我不清楚如何正确配置乐观锁定。
<!-- All table and view columns that are used as "version" fields for
optimistic locking (A Java regular expression. Use the pipe to separate several expressions).
See UpdatableRecord.store() and UpdatableRecord.delete() for details -->
<recordVersionFields>REC_VERSION</recordVersionFields>
及以下
recordVersionFields: Relevant methods from super classes are overridden to return the VERSION field
这到底是什么意思?有人可以举个例子吗?
想象一下,如果有这个table,例如:
CREATE TABLE "users" (
"id" INTEGER DEFAULT nextval('global_seq') NOT NULL,
"code" TEXT NOT NULL,
"version" INTEGER NOT NULL
);
我应该把什么放到 recordVersionFields
到 pom.xml 中?
<database>
<inputSchema>PUBLIC</inputSchema>
<recordVersionFields>users.version</recordVersionFields>
</database>
users.version
, version
, RECORD_VERSION
?
我用的是H2数据库,如果只设置version
会出现编译错误
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /D:/JooqDemo/target/generated-sources/jooq-h2/org/jooqdemo/generated/tables/SchemaVersion.java:[152,52] getRecordVersion() in org.jooqdemo.generated.tables.SchemaVersion cannot implement getRecordVersion() in org.jooq.Table
return type org.jooq.TableField<org.jooqdemo.generated.tables.records.SchemaVersionRecord,java.lang.String> is not compatible with org.jooq.TableField<org.jooqdemo.generated.tables.records.SchemaVersionRecord,? extends java.lang.Number>
您可以提供给代码生成器的所有这些正则表达式模式都是匹配模式、tables 或列的模式,它们的 unqualified 名称 (例如 version
) 或他们的 完全限定 名称(例如 public.users.version
)。您不能使用部分限定的名称,例如 users.version
.
为了回答您的具体问题,以下是可行的示例:
<!-- Will match version columns in all tables -->
<recordVersionFields>version</recordVersionFields>
<!-- Will match version columns in all user tables.
Notice that schema names are matched by .* -->
<recordVersionFields>.*\.user\.version</recordVersionFields>
<!-- Will match version columns only in that particular public.user table -->
<recordVersionFields>public\.user\.version</recordVersionFields>
这会将相关元信息生成到您的 UserRecord
中,以便在 UserRecord.store()
、update()
和 delete()
调用中启用乐观锁定。
旁注:您的编译错误
在你的问题中,你提到了一个似乎发生的编译错误,因为你正在匹配所有 table 中的所有版本列:
<!-- Will match version columns in all tables -->
<recordVersionFields>version</recordVersionFields>
这也将匹配 "wrong" 版本列,例如在 schema_version
table 中,它的类型是 VARCHAR
而不是 INTEGER
。也许,您应该更明确地使用正则表达式来进行精确匹配。
旁注:区分大小写
所有这些正则表达式都区分大小写。如果您使用的是 PostgreSQL,则以下内容可能是错误的:
<inputSchema>PUBLIC</inputSchema>
相反,写下这些:
<!-- use lower-case in PostgreSQL by default -->
<inputSchema>public</inputSchema>
<!-- use case-insensitive regular expressions to "stay safe" -->
<inputSchema>(?i:PUBLIC)</inputSchema>