3

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?

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
Ole
  • 41,793
  • 59
  • 191
  • 359
  • This type question already answered here https://stackoverflow.com/questions/4935095/jpa-hibernate-many-to-many-cascading but your is just modifiable confusing double ?? mark for your knowledge. –  Oct 08 '17 at 01:34
  • Well, let me see, if you are attaching a detached object (which was subsequently modified whilst detached) by calling `em.merge` and want to cascade the merge to related objects, perhaps you need to know whether to merge a field? which applies to 1-1, 1-N, N-1, M-N all equally. Any basic JPA doc would tell you this –  Oct 08 '17 at 06:28

0 Answers0