@Embeddable与@ManyToOne

时间:2018-01-11 11:03:34

标签: hibernate jpa embeddable

想象一下以下课程

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table style="width:100%">
  <tr class="click">
    <th>Click</th>
    <th>Click</th>
    <th class="noclick">Nothing should happen here</th>
  </tr>
</table>

所以我希望数据库表看起来像

  • TEST
    • ID
    • ...
    • EMBEDDED1_ID
    • EMBEDDED1_SOMEE​​NUM
    • EMBEDDED2_ID
    • EMBEDDED2_SOMEE​​NUM

但是如果失败了:

  

引起:org.hibernate.MappingException:映射中的重复列   for entity:ClassB列:classB_id(应映射为   insert =“false”update =“false”)

版本:

Hibernate:5.2.12.FINAL JPA:hibernate-jpa-2.1-api

1 个答案:

答案 0 :(得分:1)

您无法使用@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;