簡體   English   中英

Hibernate-一對一關系單表繼承

[英]Hibernate - One to one relation single table inheritance

我有3個實體BaseFooFooAbcFooAbcDetail FooAbc擴展了基本實體BaseFoo 我正在嘗試在FooAbcFooAbcDetail之間FooAbc一對一的關系。

@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。發布

該錯誤告訴您foo_abc_detail表上沒有名為foo_idfoo_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
}

我解決了。 我不知道這是否是最好的方法,但是可以! 我通過@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;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM