I am able to successfully add the an association between Parent and Child, however I can't seem to be able to remove it.
Project.class
Code:
public class Project {
// other properties
@JsonIgnore
@ManyToMany(mappedBy = "projectsMemberOf",
cascade = {CascadeType.PERSIST, CascadeType.MERGE},
fetch = FetchType.LAZY
)
public List<User> getProjectMembers() {
return projectMembers;
}
public void setProjectMembers(List<User> projectMembers) {
this.projectMembers = projectMembers;
}
}
User.class
Code:
public class User {
// other properties
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(name = "userProjects",
joinColumns = @JoinColumn(name = "userId", referencedColumnName = "userId"),
inverseJoinColumns = @JoinColumn(name = "projectId", referencedColumnName = "projectId"),
foreignKey = @ForeignKey(name = "FK_userProjects_userId"),
inverseForeignKey = @ForeignKey(name = "FK_userProjects_projectId")
)
public List<Project> getProjectsMemberOf() {
return projectsMemberOf;
}
private void setProjectsMemberOf(List<Project> projectsMemberOf) {
this.projectsMemberOf = projectsMemberOf;
}
}
can save using:
Code:
project.getProjectMembers().add(user);
user.getProjectsMemberOf().add(project);
em.merge(project);
however the following fails to remove the association:
Code:
project.getProjectMembers().remove(user);
user.getProjectsMemberOf().remove(project);
em.merge(project);