Suppose we have a Post
class with a tags reference that is a List
:like this (Original source):
@ManyToMany(cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
})
@JoinTable(name = "post_tag",
joinColumns = @JoinColumn(name = "post_id"),
inverseJoinColumns = @JoinColumn(name = "tag_id")
)
private List<Tag> tags = new ArrayList<>();
Why do we need CascadeType.MERGE
? Is this in the event that, for example, we remove tags from the list and then we persist the post, thus we want the result to "Merge" (Unmap the tags from the post in the many to many join table) with the current database state?