gpt4 book ai didi

java - 如何在jOOQ中配置乐观锁?

转载 作者:行者123 更新时间:2023-11-30 02:53:54 25 4
gpt4 key购买 nike

来自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

这实际上意味着什么?有人可以举个例子吗?

想象一下如果有这个表,例如:

CREATE TABLE "users" (
"id" INTEGER DEFAULT nextval('global_seq') NOT NULL,
"code" TEXT NOT NULL,
"version" INTEGER NOT NULL
);

我应该将什么内容放入 pom.xml 中的 recordVersionFields 中?

<database>
<inputSchema>PUBLIC</inputSchema>
<recordVersionFields>users.version</recordVersionFields>
</database>

users.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>

最佳答案

您可以提供给代码生成器的所有这些正则表达式模式都是通过其非限定名称(例如版本)来匹配架构、表或列的模式或通过其完全限定名称(例如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() 调用。

旁注:您的编译错误

在您的问题中,您提到了一个编译错误,该错误似乎是由于您匹配所有表中的所有版本列而发生的:

<!-- Will match version columns in all tables -->
<recordVersionFields>version</recordVersionFields>

这也将匹配“错误”的版本列,例如在 schema_version 表中,其类型为 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>

关于java - 如何在jOOQ中配置乐观锁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37817260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com