Let's say I have two entities Group and User. Evety user can vbe member of many groups and every group can have many users.
@Entity
public class User {
@ManyToMany
Set<Group> groups;
//...
}
@Entity
public class Group {
@ManyToMany(mappedBy="groups")
Set<User> users;
//...
}
Now I want to remove group (let's say it has many members).
Problem is that when I call EntityManager.remove() on some Group, JPA provider (in my case Hibernate) doesn't remove rows from join table and delete operation fails due to foreign key constrains. Calling remove() on User works fine (I guess this has something to do with owning side of relationship).
So how can I remove group in this case?
Only way I could come up with is to load all users in group, then for every user remove current group from groups and update user. But it seems ridiculous to me to call update() on every user from group just to be able to delete this group.