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

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

问题描述

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

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).

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

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?

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

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.

推荐答案

  • 关系的所有权取决于您将mappedBy"属性放置到注释的位置.您放置mappedBy"的实体不是所有者.双方都没有机会成为所有者.如果您没有删除用户"用例,您可以简单地将所有权移至 Group 实体,因为当前 User 是所有者.
  • 另一方面,您还没有问过这个问题,但有一件事值得了解.groupsusers 不相互组合.我的意思是,从 Group1.users 中删除 User1 实例后,User1.groups 集合不会自动更改(这对我来说很奇怪),
  • 总而言之,我建议您决定谁是所有者.假设 User 是所有者.然后当删除用户时,关系用户组将自动更新.但是在删除组时,您必须像这样自己删除关系:
    • 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天全站免登陆