【发布时间】:2019-08-22 09:03:47
【问题描述】:
我有 3 个实体 BaseFoo、FooAbc、FooAbcDetail。 FooAbc 扩展基本实体 BaseFoo。我正在尝试在FooAbc 和FooAbcDetail 之间建立一对一的关系。
@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:无法找到具有逻辑的列 名称:org.hibernate.mapping.Table(public.foo_abc_detail) 中的 id 及其 相关的超表和辅助表
这里有什么问题?
环境
- 休眠 5.3.10.Final
- Spring Boot 2.1.7.RELEASE
【问题讨论】:
-
展示您如何设置
foo_abc_detail表 -
@XtremeBaumer 抱歉,我无法理解。你说的设置是什么意思?是 DDL 吗?
-
正是我们需要的
-
@XtremeBaumer 对不起,我迟到了。这里是。
CREATE TABLE foo_abc_detail ( foo_id bigint NOT NULL CONSTRAINT pk_foo_abc_detail PRIMARY KEY CONSTRAINT "fk_AwfsRYAAxLwavrnv16Re" REFERENCES foo ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE --other columns );
标签: java hibernate one-to-one single-table-inheritance