字段替换
适用于 ❌ 开源版 ✅ 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)
)
)
)
import org.jooq.meta.jaxb.*
configuration {
generator {
database {
embeddables {
embeddable {
name = "MONETARY_AMOUNT"
fields {
field {
expression = "AMOUNT"
}
field {
expression = "CURRENCY"
}
}
isReplacesFields = true
}
}
}
}
}
有关更多详细信息,请参阅配置 XSD和gradle 代码生成。
configuration {
generator {
database {
embeddables {
embeddable {
name = "MONETARY_AMOUNT"
fields {
field {
expression = "AMOUNT"
}
field {
expression = "CURRENCY"
}
}
replacesFields = true
}
}
}
}
}
有关更多详细信息,请参阅配置 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.
一如既往,当使用正则表达式时,它们是具有默认标志的正则表达式。
...我们将得到一个如下所示的表
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 * 查询时,我们也不会再得到这些字段。
反馈
您对此页面有任何反馈吗? 我们很乐意听到!