【问题标题】:How to add a foreign key constraint with the annotations如何使用注释添加外键约束
【发布时间】:2021-09-22 14:01:50
【问题描述】:

我有两个实体ParentEntityChildEntity,其中ChildEntity 引用ParentEntity。这是一个多(子)对一(父)关系,定义如下:

@MappedEntity
public record ChildEntity (
    @Id Long id,
    String name,
    @Relation(Kind.MANY_TO_ONE) ParentEntity parent)  {
}

@MappedEntity
public record ParentEntity (@Id Long id,  
                            String name) {
}

Micronaut Data 在创建架构时执行以下 SQL 查询:

创建表parent_entity (id BIGINT NOT NULL,name VARCHAR(255) 非空);

创建表child_entity (id BIGINT NOT NULL,name VARCHAR(255) NOT NULL,parent_id BIGINT NOT NULL);

我期望(或希望)添加外键约束以强制引用完整性,因此 parent_id 必须始终具有 parentEntity.id 的值

如何在 MappedEntity 类(ChildEntity、ParentEntity 等)上执行此操作,同时仍使用 Micronaut Data JDBC? (没有 Hibernate 或 JPA 等)

【问题讨论】:

    标签: java micronaut micronaut-data


    【解决方案1】:

    你需要使用flyway之类的数据库迁移工具来创建合适的表。

    【讨论】:

      猜你喜欢
      • 2013-03-03
      • 2019-07-26
      • 2017-06-10
      • 2014-06-01
      • 1970-01-01
      • 2014-09-28
      • 2017-03-25
      • 2018-04-01
      • 1970-01-01
      相关资源
      最近更新 更多