使用JPA映射双向一对多Hibernate实体

时间:2016-05-18 17:27:53

标签: java hibernate jpa spring-boot

我试图在Hibernate中映射双向一对多关系。在构建日志中,我收到错误“实体映射中的重复列。”

产生错误的原因是什么?

实体源代码如下。一个有复合主键。我正在使用Lombok生成getter和setter。

关系:奖励(一) - > AwardReceived(很多)

奖励实体

@Entity
@Table(name = "awards")
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class Award implements Serializable {

    @Id
    @Column(name = "award_id")
    private Long awardId;

    @OneToMany(cascade=CascadeType.ALL, mappedBy = "award")
    private Set<AwardReceived> awardsReceived;

    @Column(name = "award_type")
    private String awardType;

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

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

}

AwardReceived Entity

@Entity
@Table(name = "awards_received")
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class AwardReceived implements Serializable{

    @EmbeddedId
    @JsonUnwrapped
    private AwardReceivedPk awardReceivedPk;

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name = "award_id")
    private Award award;

    @Column(name = "award_name")
    private String awardName;

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

}

AwardReceivedPk

@Embeddable
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class AwardReceivedPk implements Serializable{

    @JsonIgnore
    @Column(name = "client_no")
    private String clientNo;

    @Column(name = "award_id")
    private Long awardId;

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

}

1 个答案:

答案 0 :(得分:2)

请尝试

@ManyToOne(cascade=CascadeType.ALL)
private Award award;

而不是

@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "award_id")
private Award award;