JPA休眠多对多级联 [英] JPA Hibernate many-to-many cascading

查看:113
本文介绍了JPA休眠多对多级联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JPA 2.0和hibernate。我有一个User类和一个Group类,如下所示:

  public class User实现Serializable {
@Id
@Column(name =USER_ID)
private String userId;

@ManyToMany
@JoinTable(name =USER_GROUP,
joinColumns = {
@JoinColumn(name =GROUP_ID)
},
inverseJoinColumns = {
@JoinColumn(name =USER_ID)
}

private Set< Group>群组列表;

//获取设置方法
}

public class Group
{
@Id
@Column(name = GROUP_ID)
private String groupId;

@ManyToMany(mappedBy =groupList)
private Set< User>会员;
//获取设置方法
}

然后,我创建一个用户然后将该用户分配给该组。



我想要的是当我删除该组时,该组将被删除(当然),并且所有用户组关系将从USER_GROUP连接表中自动删除,但用户本身不会从USER表中删除。



使用上面的代码,当我删除一个组时,只有GROUP表中的行将被删除,并且用户仍然会在USER_GROUP连接表中拥有一个删除组的条目。



如果我在这样的User类中放置了级联:

  @ManyToMany(cascade = CascadeType.ALL)
@JoinTable( name =USER_GROUP,
joinColumns =
{
@JoinColumn(name =GROUP_ID)
},
inverseJoinColumns =
{
@JoinColumn(name =USER_ID)
})
private设置< Group>群组列表;

当我删除群组时,用户也会被删除!



有什么方法可以实现我想要的吗?

解决方案

User 是关系的管理方,因此它将负责更新连接表。
$ b $

用户映射从 JoinTable 映射更改为 Group ,并使 group 属性为 User ,这样它就拥有了 mappedBy 属性。这会将组更改为关系的管理方,并且对该组进行持续/更新调用来管理联接表。



但是请注意,您不能简单地向用户添加组,保存用户并继续,您必须添加一个用户加入该组并保存该组以查看更改,但双向多对多希望您能密切管理该关系。


I am using JPA 2.0 and hibernate. I have a User class and a Group class as follows:

public class User implements Serializable {
    @Id
    @Column(name="USER_ID")
    private String userId;

    @ManyToMany
    @JoinTable(name = "USER_GROUP",
               joinColumns = {
                   @JoinColumn(name = "GROUP_ID")
               },
               inverseJoinColumns = {
                   @JoinColumn(name = "USER_ID")
               }
    )
    private Set<Group> groupList;

    //get set methods
}

public class Group
{
    @Id
    @Column(name="GROUP_ID")
    private String groupId;

    @ManyToMany(mappedBy="groupList")
    private Set<User> memberList;
    //get set methods
}

And then, I create a user and group and then assign the user to the group.

What I want to have is when I delete the group, the group will be deleted (of course) and all the user-group relationship that the group has will be automatically deleted from the USER_GROUP join table but the user itself is not deleted from the USER table.

With the code I have above, only the row in the GROUP table will be deleted when I delete a group and the user will still have an entry to the deleted group in the USER_GROUP join table.

If I put cascade in the User class like this:

@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name = "USER_GROUP",
joinColumns =
{
    @JoinColumn(name = "GROUP_ID")
},
inverseJoinColumns =
{
    @JoinColumn(name = "USER_ID")
})
private Set<Group> groupList;

When I delete the group, the user will be deleted as well!

Is there any way to achieve what I want?

解决方案

The way you have it mapped, the User is the managing side of the relationship, therefore it will be responsible for updating the join table.

Change the JoinTable mapping from User to Group, and make the groupList property of User so it has the mappedBy attribute. That will change the group to the managing side of the relationship, and make persist/update calls to the group manage the join table.

But take note of that, you won't be able to simply add a group to a user, save the user, and continue, you'll instead have to add a user to the group and save the group to see the changes, but having the bi-directional many-to-many hopefully you'll be closely managing that relationship anyway.

这篇关于JPA休眠多对多级联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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