【问题标题】:Hibernate - One to one relation single table inheritanceHibernate - 一对一关系单表继承
【发布时间】:2019-08-22 09:03:47
【问题描述】:

我有 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:无法找到具有逻辑的列 名称: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


【解决方案1】:

此错误告诉您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
}

【讨论】:

  • 请提供表foo_abc_detail的列结构
【解决方案2】:

好吧,我解决了。我不知道这是否是最好的方法,但它有效!我通过@JoinColumn 替换了@MapsId 注释。 FooAbcDetail 类的最终结果如下。

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

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

    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id", referencedColumnName = "id", nullable = false)
    private FooAbc fooAbc;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 2011-10-10
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    相关资源
    最近更新 更多