gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:34:57 25 4
gpt4 key购买 nike

我有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com