java - Hibernate - 一对一关系单表继承

标签 java hibernate one-to-one single-table-inheritance

我有 3 个实体 BaseFooFooAbcFooAbcDetailFooAbc 扩展了基本实体 BaseFoo。我正在尝试在 FooAbcFooAbcDetail 之间建立一对一的关系。

@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "foo")
@Audited
@AuditOverride(forClass = Auditable.class)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type_id", discriminatorType = DiscriminatorType.INTEGER)
public abstract class BaseFoo extends Auditable implements Serializable {

    @Id
    @Column(name = "id")
    private Long id;

    //other fields
}

@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Audited
@AuditOverride(forClass = BaseFoo.class)
@DiscriminatorValue("2")
public class FooAbc extends BaseFoo implements Serializable {

    @EqualsAndHashCode.Exclude
    @ToString.Exclude
    @NotAudited
    @OneToOne(mappedBy = "fooAbc",
              fetch = FetchType.LAZY,
              cascade = CascadeType.ALL,
              orphanRemoval = true,
              optional = false)
    private FooAbcDetail fooAbcDetail;

    //other fields
}

@Data
@Entity
@Table(name = "foo_abc_detail")
public class FooAbcDetail implements Serializable {

    @Id
    @Column(name = "foo_id"/* foo_abc_id (I tried both) */)
    private Long id;

    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    @MapsId //also tried @MapsId("id")
    @OneToOne(fetch = FetchType.LAZY)
    private FooAbc fooAbc; //also tried BaseFoo

    //other fields
}

项目启动 Hibernate 时抛出:

org.hibernate.MappingException: Unable to find column with logical name: id in org.hibernate.mapping.Table(public.foo_abc_detail) and its related supertables and secondary tables

这里有什么问题吗?

环境

  • hibernate 5.3.10.Final
  • Spring Boot 2.1.7.RELEASE

最佳答案

此错误告诉您 foo_abc_detail 表上没有名为 foo_id 的列。foo_abc_detail 上的列仅名为 id.

@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "foo")
@Audited
@AuditOverride(forClass = Auditable.class)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type_id", discriminatorType = DiscriminatorType.INTEGER)
public abstract class BaseFoo extends Auditable implements Serializable {

    @Id
    @Column(name = "id")
    private Long id;

    //other fields
}

@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Audited
@AuditOverride(forClass = BaseFoo.class)
@DiscriminatorValue("2")
public class FooAbc extends BaseFoo implements Serializable {

    @EqualsAndHashCode.Exclude
    @ToString.Exclude
    @NotAudited
    @OneToOne(mappedBy = "fooAbc",
              fetch = FetchType.LAZY,
              cascade = CascadeType.ALL,
              orphanRemoval = true,
              optional = false)
    private FooAbcDetail fooAbcDetail;

    //other fields
}

@Data
@Entity
@Table(name = "foo_abc_detail")
public class FooAbcDetail implements Serializable {

    @Id
    @Column(name = "id")
    private Long id;

    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    @MapsId //also tried @MapsId("id")
    @OneToOne(fetch = FetchType.LAZY)
    private FooAbc fooAbc; //also tried BaseFoo

    //other fields
}

关于java - Hibernate - 一对一关系单表继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57605711/

相关文章:

java.lang.IllegalArgumentException : Document base dir does not exist or is not a readable directory 异常

java - 如何使用 Spring 实现自定义 WebSocket 子协议(protocol)

java - 使用 SessionFactoryUtils 时应该关闭连接吗

hibernate - 具有复合主键的一对一双向映射

java - Spark-Kafka-Streaming : Offset Management - Can't get manual commit to work (Java)

java - servlet 登录应用程序中未找到列异常

sql - node.js Sequelize 交易

java - Hibernate OneToOne 的行为类似于 ManyToOne

java - hibernate 查询以获取服务器时间而不转换为本地时区

java - 无法生成 hibernate.reveng.xml,因为 hibernate.connection.url 是动态的