Let's say I have two entities: Group and User. Every user can be 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 a 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) does not 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 a group in this case?
Only way I could come up with is to load all users in the group, then for every user remove current group from his groups and update user. But it seems ridiculous to me to call update() on every user from the group just to be able to delete this group.
If you are using Spring Data Jpa, then simply create a repository interface for the owner class Group. class , then use their deleteById(Long id) method extended from JpaRepository. class .
Group
entity, as currently the User
is the owner.groups
and users
are not combined with each other. I mean, after deleting User1 instance from Group1.users, the User1.groups collections is not changed automatically (which is quite surprising for me),User
is the owner. Then when deleting a user the relation user-group will be updated automatically. But when deleting a group you have to take care of deleting the relation yourself like this:entityManager.remove(group) for (User user : group.users) { user.groups.remove(group); } ... // then merge() and flush()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With