Hibernate - 更新@OneToMany列时重复输入

时间:2017-05-03 07:10:09

标签: java mysql hibernate

我有userInfo实体和ChannelDetails实体

public class UserInfoDO implements Serializable {

    @Column(name = "id")
    @Id
    @GeneratedValue
    private int id;
    @Column(name = "`type`")
    private String type;
    @Column(name = "`value`")
    private String value;
    @Column(name = "created_on")
    private Date createdOn; 

    @OneToMany(orphanRemoval = true, mappedBy = "identifierId", fetch = FetchType.EAGER)
    private Set<ChannelDetailsDO> channelDetails = new HashSet<ChannelDetailsDO>();
}

public class ChannelDetailsDO {

    @Column(name = "id")
    @Id
    @GeneratedValue
    private int id;
    @Column(name = "channel")
    private String channel;
    @Column(name = "is_blocked")
    private int isBlocked;
    @Column(name = "blocking_reason")
    private String blockingReason;
    @Column(name = "created_on")
    private Date createdOn;
    @Column(name = "updated_on")
    private Date updatedOn;

    @ManyToOne(targetEntity = UserInfoDO.class, cascade = CascadeType.MERGE)
    @JoinColumn(name = "identifier_id")
    private UserInfoDO identifierId;
}

有一个更新场景,对于UserInfo,可以更新,添加频道

例如:对于id = 1且值= 2222的用户, channelDetails是[{channel:123,isblocked:true,blockReason:&#34; some&#34;},{channel:124,isblocked:true,blockReason:&#34; some&#34;}]

现在我想用124的频道详细信息更新用户,并且平行想要再添加一个channelDetail 125.当我这样做时。为124创建重复行。为了解决这个问题,我在通道列上保留了唯一约束,但是125没有添加错误: 在ChannelDetailsDO条目中为null id(在发生异常后不要刷新会话)

需要进行哪些更改才能更新ChannelDetails以及如果尚未在DB中添加它们。

0 个答案:

没有答案