如何在JPA(和相应的连接表行)中删除具有ManyToMany关系的实体? [英] How to remove entity with ManyToMany relationship in JPA (and corresponding join table rows)?

查看:453
本文介绍了如何在JPA(和相应的连接表行)中删除具有ManyToMany关系的实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个实体:组和用户。每个用户可以是许多组的成员,每个组可以有很多用户。

  @Entity 
public class User {
@ManyToMany
Set< Group>组;
// ...
}

@实体
公共类组{b $ b @ManyToMany(mappedBy =groups)
Set< ;使用者>用户;
// ...
}

现在我想删除一个组(假设它有许多成员)。



问题是,当我在某个Group上调用EntityManager.remove()时,JPA提供者(在我的情况下为Hibernate)不会从连接表中删除行,并且由于外键约束,删除操作将失败。在用户上调用remove()会正常工作(我想这与拥有关系的一方有关)。

那么在这种情况下如何删除一个组?

只有我能想到的方式是加载组中的所有用户,然后为每个用户从他的组中删除当前组并更新用户。但是,对于我来说,对组中的每个用户调用update()只是为了能够删除这个组,这似乎很荒谬。

解决方案


  • 关系的所有权取决于您将'mappedBy'属性放置到注释的位置。您放置'mappedBy'的实体不是所有者。双方都不可能成为所有者。如果您没有删除用户用例,您可以简单地将所有权移至 Group 实体,如 User code>是所有者。
  • 另一方面,您没有问过这个问题,但有一点值得了解。 用户不会相互组合。我的意思是,从Group1.users中删除User1实例后,User1.groups集合不会自动更改(这对我来说非常令人惊讶),总而言之,我建议您决定谁是主人。假设 User 是所有者。然后,在删除用户时,关系用户组将自动更新。但是,当删除一个组时,你必须小心地删除这个关系,像这样:






< pre $ entityManager.remove(group)
for(User user:group.users){
user.groups.remove(group);
}
...
//然后merge()和flush()


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.

解决方案

  • The ownership of the relation is determined by where you place the 'mappedBy' attribute to the annotation. The entity you put 'mappedBy' is the one which is NOT the owner. There's no chance for both sides to be owners. If you don't have a 'delete user' use-case you could simply move the ownership to the Group entity, as currently the User is the owner.
  • On the other hand, you haven't been asking about it, but one thing worth to know. The 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),
  • All in all, I would suggest you decide who is the owner. Let say the 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()

这篇关于如何在JPA(和相应的连接表行)中删除具有ManyToMany关系的实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆