可用版本: Dev (3.21) | 最新 (3.20) | 3.19 | 3.18 | 3.17 | 3.16 | 3.15 | 3.14

合成外键

适用于 ❌ 开源版   ✅ Express 版   ✅ 专业版   ✅ 企业版

jOOQ 的代码生成器可以识别由数据库声明和报告的外键。但是,一些数据库不会报告所有键,或者某些表未启用键,或者有时视图是底层表的 1:1 表示,但它不公开键信息。在这些情况下,此正则表达式可以匹配用户希望“假装”为外键一部分的所有列。如果需要合成复合外键,则正则表达式应匹配该表中属于外键的所有列。例如,合成复合外键由 SCHEMA.TABLE 表中的 (COLUMN1, COLUMN2) 组成

XML(独立和 Maven)
编程方式
Gradle (Kotlin)
Gradle (Groovy)
Gradle(第三方)
<configuration>
  <generator>
    <database>
      <syntheticObjects>
        <foreignKeys>
          <foreignKey>

            <!-- Optional name of the foreign key, if tables matches only a single table. -->
            <!-- This is useful to disambiguate navigational methods and implicit join methods! -->
            <name>FK_TABLE</name>

            <!-- Optional regular expression matching all tables that have this foreign key. -->
            <tables>SCHEMA\.TABLE</tables>

            <!-- List multiple fields in the key order -->
            <fields>
              <field>COLUMN1</field>
              <field>COLUMN2</field>
            </fields>

            <!-- Specify the table that is being referenced by this foreign key -->
            <referencedTable>SCHEMA\.OTHER_TABLE</referencedTable>

            <!-- Optional: The primary or unique key columns that are being referenced -->
            <referencedFields>
              <field>COLUMN1</field>
              <field>COLUMN2</field>
            </referencedFields>

            <!-- Optional: reference a primary or unique key by name -->
            <!-- If the referenced fields or key are not specified, the referencedTable's primary key is used -->
            <referencedKey>UK_TABLE</referencedKey>
          </foreignKey>
        </foreignKeys>
      </syntheticObjects>
    </database>
  </generator>
</configuration>

有关更多详细信息,请参见 配置 XSD独立代码生成maven 代码生成

new org.jooq.meta.jaxb.Configuration()
  .withGenerator(new Generator()
    .withDatabase(new Database()
      .withSyntheticObjects(new SyntheticObjectsType()
        .withForeignKeys(
          new SyntheticForeignKeyType()

            // Optional name of the foreign key, if tables matches only a single table.
            .withName("FK_TABLE")

            // Optional regular expression matching all tables that have this foreign key.
            .withTables("SCHEMA\\.TABLE")

            // List multiple fields in the key order
            .withFields(
              "COLUMN1",
              "COLUMN2"
            )

            // Specify the table that is being referenced by this foreign key
            .withReferencedTable("SCHEMA\\.OTHER_TABLE")

            // Optional: The primary or unique key columns that are being referenced
            .withReferencedFields(new ()
              .withField("COLUMN1")
              .withField("COLUMN2")
            )

            // Optional: reference a primary or unique key by name
            .withReferencedKey("UK_TABLE")
        )
      )
    )
  )

有关更多详细信息,请参见 配置 XSD程序化代码生成

import org.jooq.meta.jaxb.*


configuration {
  generator {
    database {
      syntheticObjects {
        foreignKeys {
          foreignKey {

            // Optional name of the foreign key, if tables matches only a single table.
            name = "FK_TABLE"

            // Optional regular expression matching all tables that have this foreign key.
            tables = "SCHEMA\\.TABLE"

            // List multiple fields in the key order
            fields {
              field = "COLUMN1"
              field = "COLUMN2"
            }

            // Specify the table that is being referenced by this foreign key
            referencedTable = "SCHEMA\\.OTHER_TABLE"

            // Optional: The primary or unique key columns that are being referenced
            referencedFields {
              field = "COLUMN1"
              field = "COLUMN2"
            }

            // Optional: reference a primary or unique key by name
            referencedKey = "UK_TABLE"
          }
        }
      }
    }
  }
}

有关更多详细信息,请参见 配置 XSDgradle 代码生成

configuration {
  generator {
    database {
      syntheticObjects {
        foreignKeys {
          foreignKey {

            // Optional name of the foreign key, if tables matches only a single table.
            name = "FK_TABLE"

            // Optional regular expression matching all tables that have this foreign key.
            tables = "SCHEMA\\.TABLE"

            // List multiple fields in the key order
            fields {
              field = "COLUMN1"
              field = "COLUMN2"
            }

            // Specify the table that is being referenced by this foreign key
            referencedTable = "SCHEMA\\.OTHER_TABLE"

            // Optional: The primary or unique key columns that are being referenced
            referencedFields {
              field = "COLUMN1"
              field = "COLUMN2"
            }

            // Optional: reference a primary or unique key by name
            referencedKey = "UK_TABLE"
          }
        }
      }
    }
  }
}

有关更多详细信息,请参见 配置 XSDgradle 代码生成

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

一如既往,当使用正则表达式时,它们是具有默认标志的正则表达式

反馈

您对此页面有任何反馈吗?我们很乐意听到!

The jOOQ Logo