如何在Embeddedid中使用@OnetoMany

时间:2019-03-30 12:38:35

标签: java hibernate

我正在休眠并绑定以将具有@OneToMany关系的映射与复合键相关联。 以下是我目前正在使用的实体。

@Embeddable
@Getter
@Setter
public class AddressKey implements Serializable {

    private static final long serialVersionUID = -307823488229761699L;

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

    @Column(name = "city")
    private Long city;

    @Column(name = "locale")
    private String locale;

    @Column(name = "type")
    private String type;

    @ManyToOne
    @JoinColumn(name="id")
    private Person person;
}

@Entity
@Table(name = "address", schema = "test")
@Setter
@Getter
public class AddressHistory {

    @EmbeddedId
    private AddressKey key;

    @Column(name = "active")
    private boolean active;

    @Column(name = "current")
    private boolean current;

}


@Entity
@Table(name = "person", schema="test")
@ToString
public class Person {

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

    @OneToMany(mappedBy="key.person", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    private Set<AddressHistory> addressHistory;
}

但是,当我尝试运行该程序时,出现以下错误。

实体AddressHistory映射中的

重复列。

有人帮助我解决了此映射中的错误。 预先感谢

1 个答案:

答案 0 :(得分:0)

您重复了各列。删除AddressKey中的@JoinColumn(name =“ id”),因为您已经有一个具有相同名称的列,或者将其重命名为其他名称,并且更易于维护,例如person_id。