2

想象一下上课

@Embeddable
class A {
    @ManyToOne
    public B classB;

    ...
    public State someEnum;
}

@Entity
@Table(name = "TEST")
class B {
    public long id;
    //... some data


   @Embedded
   @AttributeOverrides({
        @AttributeOverride(
                name = "classB.id",
                column = @Column(name = "EMBEDDED1_ID")
        ),
        @AttributeOverride(
                name = "someEnum",
                column = @Column(name = "EMBEDDED1_SOMEENUM")
        )
   })
   public A embedded1;

   @Embedded
   @AttributeOverrides({
        @AttributeOverride(
                name = "classB.id",
                column = @Column(name = "EMBEDDED2_ID")
        ),
        @AttributeOverride(
                name = "someEnum",
                column = @Column(name = "EMBEDDED2_SOMEENUM")
        )
   })
   public A embedded2;
}

所以我期待数据库表看起来像

  • 测试
    • ID
    • ...
    • EMBEDDED1_ID
    • EMBEDDED1_SOMEE​​NUM
    • EMBEDDED2_ID
    • EMBEDDED2_SOMEE​​NUM

但如果失败:

原因:org.hibernate.MappingException:实体映射中的重复列:ClassB 列:classB_id(应使用 insert="false" update="false" 进行映射)

版本:

休眠:5.2.12.FINAL JPA:休眠-jpa-2.1-api

4

1 回答 1

4

您不能使用 @AttributeOverride 重命名外键列。你必须使用@AssosiactionOverride

@Entity
@Table(name = "TEST")
public class B {

    public long id;

    @AssociationOverride(name = "classB", joinColumns = @JoinColumn(name = "EMBEDDED1_ID"))
    @AttributeOverrides({
            @AttributeOverride(
                    name = "someEnum",
                    column = @Column(name = "EMBEDDED1_SOMEENUM")
            )
    })
    @Embedded
    public A embedded1;

    @AssociationOverride(name = "classB", joinColumns = @JoinColumn(name = "EMBEDDED2_ID"))
    @AttributeOverrides({
            @AttributeOverride(
                    name = "someEnum",
                    column = @Column(name = "EMBEDDED2_SOMEENUM")
            )
    })
    @Embedded
    public A embedded2;
于 2018-01-11T12:30:07.703 回答