在JPA中,One To Many字段不会更新

时间:2016-05-16 16:59:16

标签: java hibernate jpa

@Entity
public class Conference {

    @ManyToOne
    @JoinColumn(name = "host_id")
    @JsonManagedReference
    private User host;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "PARTICIPANT_CONFERENCE",
            joinColumns = @JoinColumn(name = "CONFERENCE_ID_FRK"),
            inverseJoinColumns = @JoinColumn(name = "PARTICIPANT_ID_FRK")
    )
    @JsonManagedReference
    private Set<User> participants;
}

@Entity
public class User {
    @ManyToMany(
            targetEntity = Conference.class,
            fetch = FetchType.EAGER,
            cascade = CascadeType.REMOVE,
            mappedBy = "participants"
    )
    @JsonBackReference
    private Set<Conference> conferenceSet;

    @OneToMany(
            targetEntity = Conference.class,
            mappedBy = "host",
            cascade = CascadeType.REMOVE,
            fetch = FetchType.EAGER
    )
    @JsonBackReference
    private Set<Conference> conferenceHostSet;
}

我使用了JPA,但是在JPA中有一个OneTOMany字段没有更新的问题。

如果我在会议中添加了主持人,但用户的主机设置未更新。

所以,我也尝试更新用户的主机集。

Set<Conference> conferences = form.getHost().getConferenceHostSet();
conferences.add(conference);
userRepository.save(form.getHost().setConferenceSet(conferences));

效果很好,但用户的参与者设置也已更新。

如何只更新主机?

1 个答案:

答案 0 :(得分:0)

您已将级联设置为仅用于“删除”,您需要为所需的所有操作进行设置。