简体   繁体   English

Hibernate-一对一关系单表继承

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

I have 3 entities BaseFoo , FooAbc , FooAbcDetail . 我有3个实体BaseFooFooAbcFooAbcDetail FooAbc extends base entity BaseFoo . FooAbc扩展了基本实体BaseFoo I'm trying to do one to one relation between FooAbc and FooAbcDetail . 我正在尝试在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
}

While project starting up Hibernate throws: 当项目启动时,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 org.hibernate.MappingException:无法在org.hibernate.mapping.Table(public.foo_abc_detail)及其相关的超表和辅助表中找到逻辑名称为ID的列

What is the problem here? 这里有什么问题?

Environment 环境

  • Hibernate 5.3.10.Final 休眠5.3.10.Final
  • Spring Boot 2.1.7.RELEASE Spring Boot 2.1.7。发布

This error is telling you that there is no column on the foo_abc_detail table called foo_id .The column on foo_abc_detail is just called id . 该错误告诉您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
}

Okey I solved. 我解决了。 I don't know if this is the best way to do it but it works! 我不知道这是否是最好的方法,但是可以! I replaced the @MapsId annotation via @JoinColumn . 我通过@JoinColumn替换了@MapsId注释。 Final result of the FooAbcDetail class like as below. 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