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

字段替换

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

上一节介绍了嵌入式类型的一般工作方式。在大多数情况下,存在嵌入式类型时,原始字段在大多数查询中不再有趣,因此您需要用嵌入式类型替换它们。

假设我们再次有一个像这样的 TRANSACTIONS

CREATE TABLE transactions (
  id BIGINT NOT NULL PRIMARY KEY,
  create_date TIMESTAMP NOT NULL,
  modified_date TIMESTAMP,
  created_by VARCHAR(100) NOT NULL,
  modified_by VARCHAR(100) NOT NULL,

  -- Other columns here
  amount DECIMAL(18, 2) NOT NULL,
  currency VARCHAR(10) NOT NULL
);

使用之前的嵌入式类型定义,以及像这样的 <replacesFields/> 标志

XML(独立和 Maven)
编程方式
Gradle (Kotlin)
Gradle (Groovy)
Gradle(第三方)
<configuration>
  <generator>
    <database>
      <embeddables>
        <embeddable>
          <name>MONETARY_AMOUNT</name>
          <fields>
            <field><expression>AMOUNT</expression></field>
            <field><expression>CURRENCY</expression></field>
          </fields>
          <replacesFields>true</replacesFields>
        </embeddable>
      </embeddables>
    </database>
  </generator>
</configuration>

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

new org.jooq.meta.jaxb.Configuration()
  .withGenerator(new Generator()
    .withDatabase(new Database()
      .withEmbeddables(
        new EmbeddableDefinitionType()
          .withName("MONETARY_AMOUNT")
          .withFields(
            new EmbeddableField()
              .withExpression("AMOUNT"),
            new EmbeddableField()
              .withExpression("CURRENCY")
          )
          .withReplacesFields(true)
      )
    )
  )

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

import org.jooq.meta.jaxb.*


configuration {
  generator {
    database {
      embeddables {
        embeddable {
          name = "MONETARY_AMOUNT"
          fields {
            field {
              expression = "AMOUNT"
            }
            field {
              expression = "CURRENCY"
            }
          }
          isReplacesFields = true
        }
      }
    }
  }
}

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

configuration {
  generator {
    database {
      embeddables {
        embeddable {
          name = "MONETARY_AMOUNT"
          fields {
            field {
              expression = "AMOUNT"
            }
            field {
              expression = "CURRENCY"
            }
          }
          replacesFields = true
        }
      }
    }
  }
}

有关更多详细信息,请参阅配置 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.

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

...我们将得到一个如下所示的表

public class Transactions extends TableImpl<TransactionsRecord> {
    // Field initialisations omitted for simplicity

    // The AUDIT embeddable and its physical fields that it represents, but the fields are not accessible
    private TableField<TransactionsRecord, Integer> ID;
    private TableField<TransactionsRecord, Timestamp> CREATED_AT;
    private TableField<TransactionsRecord, Timestamp> MODIFIED_AT;
    private TableField<TransactionsRecord, String> CREATED_BY;
    private TableField<TransactionsRecord, String> MODIFIED_BY;
    public TableField<TransactionsRecord, AuditRecord> AUDIT_REFERENCE;

    // The MONETARY_AMOUNT embeddable and its physical fields that it represents , but the fields are not accessible
    private TableField<TransactionsRecord, BigDecimal> AMOUNT;
    private TableField<DRecord, String> CURRENCY;
    public TableField<DRecord, MonetaryAmountRecord> MONETARY_AMOUNT;

不仅无法再访问这些字段(这意味着我们不能直接使用它们,它们也不会妨碍自动完成等),而且在使用 SELECT * 查询时,我们也不会再得到这些字段。

当使用嵌入式键嵌入式域时,此标志将打开,其中不需要原始字段引用。

反馈

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

The jOOQ Logo