合成主键
适用于 ✅ 开源版 ✅ 专业版 ✅ 企业版
jOOQ 的代码生成器可以识别由数据库声明并报告为主键的主键。但是,有些数据库不会报告所有键,或者有些表没有启用主键,或者有时视图是对底层表的 1:1 表示,但它不公开键信息。在这些情况下,此正则表达式可以匹配用户希望“假装”是主键一部分的所有列。如果需要复合合成主键,则正则表达式应匹配该表中属于主键的所有列。例如,复合合成主键由表 SCHEMA.TABLE
中的 (COLUMN1, COLUMN2)
组成
XML(独立和 Maven)
编程方式
Gradle (Kotlin)
Gradle (Groovy)
Gradle(第三方)
<configuration> <generator> <database> <syntheticObjects> <primaryKeys> <primaryKey> <!-- Optional name of the primary key, if tables matches only a single table. --> <name>PK_TABLE</name> <!-- Optional regular expression matching all tables that have this primary key. --> <tables>SCHEMA\.TABLE</tables> <!-- List multiple fields in the key order --> <fields> <field>COLUMN1</field> <field>COLUMN2</field> </fields> <!-- Alternatively, instead of listing columns above, promote a unique key to a primary key, by name. --> <key>UK_TABLE</key> </primaryKey> </primaryKeys> </syntheticObjects> </database> </generator> </configuration>
有关更多详细信息,请参见 configuration XSD,独立代码生成和 maven 代码生成。
new org.jooq.meta.jaxb.Configuration() .withGenerator(new Generator() .withDatabase(new Database() .withSyntheticObjects(new SyntheticObjectsType() .withPrimaryKeys( new SyntheticPrimaryKeyType() // Optional name of the primary key, if tables matches only a single table. .withName("PK_TABLE") // Optional regular expression matching all tables that have this primary key. .withTables("SCHEMA\\.TABLE") // List multiple fields in the key order .withFields( "COLUMN1", "COLUMN2" ) // Alternatively, instead of listing columns above, promote a unique key to a primary key, by name. .withKey("UK_TABLE") ) ) ) )
有关更多详细信息,请参见 configuration XSD 和 程序化代码生成。
import org.jooq.meta.jaxb.* configuration { generator { database { syntheticObjects { primaryKeys { primaryKey { // Optional name of the primary key, if tables matches only a single table. name = "PK_TABLE" // Optional regular expression matching all tables that have this primary key. tables = "SCHEMA\\.TABLE" // List multiple fields in the key order fields { field = "COLUMN1" field = "COLUMN2" } // Alternatively, instead of listing columns above, promote a unique key to a primary key, by name. key = "UK_TABLE" } } } } } }
有关更多详细信息,请参见 configuration XSD 和 gradle 代码生成。
configuration { generator { database { syntheticObjects { primaryKeys { primaryKey { // Optional name of the primary key, if tables matches only a single table. name = "PK_TABLE" // Optional regular expression matching all tables that have this primary key. tables = "SCHEMA\\.TABLE" // List multiple fields in the key order fields { field = "COLUMN1" field = "COLUMN2" } // Alternatively, instead of listing columns above, promote a unique key to a primary key, by name. key = "UK_TABLE" } } } } } }
有关更多详细信息,请参见 configuration XSD 和 gradle 代码生成。
// The jOOQ-codegen-gradle plugin has been introduced in version 3.19. // Please use the official plugin instead of the third party plugin that was recommended before.
一如既往,当使用正则表达式时,它们是具有默认标志的正则表达式。
如果正则表达式匹配表中已存在主键的列,则现有主键将被合成主键替换。但是,它仍将被报告为唯一键。
反馈
您对此页面有任何反馈吗? 我们很乐意听到您的意见!