三个表之间的@ManyToMany关系

2022-09-03 00:51:32

我在春季 JPA 应用程序中有三个独立的实体 - 用户、部门、角色

我的数据库中有一个联接表来关联以下每个实体:USER_DEPARTMENT_ROLE

我的问题是,如何在我的实体类中定义这种关系?我是否必须在每个单独的实体中定义关系?我知道如何定义两个表之间的这种关系,但对于两个以上的表,我不确定从哪里开始。@ManyToMany

任何帮助是值得赞赏的!


答案 1

如果您在连接表中映射了两个以上的关系,那么我建议创建一个单独的实体,用于映射该特定表。

问题是,您是否可以有一个不同的 id 列,该列将用作人工主键,或者您必须坚持使用从三个外键构建的复合主键。

如果您可以添加该人工id(这是设计数据库的现代方法),那么您的映射应如下所示:

备选案文1

class User {
    @OneToMany(mappedBy = "user", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    private Set<UserDepartmentRoleLink> userDepartmentRoleLinks;
}

class Department{
    @OneToMany(mappedBy = "department", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    private Set<UserDepartmentRoleLink> userDepartmentRoleLinks;
}

class Role{
    @OneToMany(mappedBy = "role", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    private Set<UserDepartmentRoleLink> userDepartmentRoleLinks;
}

class UserDepartmentRoleLink {

    @Id
    @GeneratedValue
    private Long id;

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name = "user_id")
    private User user;

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name = "department_id")
    private Department department;

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name = "role_id")
    private Role role;

}

关于为多对多关系设置级联类型是很棘手的,对于许多到许多涉及三个表的人来说甚至更棘手,因为每个实体都可以根据情况扮演父级或子级的角色。我建议只坚持使用并手动处理其他操作。cascade = {CascadeType.PERSIST, CascadeType.MERGE}

如果必须保留复合主键,则应添加其他 Id 类并将链接实体更改为以下内容:

备选案文2

class User {
    @OneToMany(mappedBy = "linkPk.user", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    private Set<UserDepartmentRoleLink> userDepartmentRoleLinks;
}

class Department{
    @OneToMany(mappedBy = "linkPk.department", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    private Set<UserDepartmentRoleLink> userDepartmentRoleLinks;
}

class Role{
    @OneToMany(mappedBy = "linkPk.role", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    private Set<UserDepartmentRoleLink> userDepartmentRoleLinks;
}

联动表

class UserDepartmentRoleLink {

    @EmbeddedId
    private UserDepartmentRoleLinkId linkPk 
          = new UserDepartmentRoleLinkId(); 

    @Transient
    public User getUser() {
        return getLinkPk().getUser();
    }

    @Transient
    public User getDepartment() {
        return getLinkPk().getDepartment();
    }

    @Transient
    public User getRole() {
        return getLinkPk().getRole();
    }    
 }

@Embeddable
public class UserDepartmentRoleLinkId implements java.io.Serializable {

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name = "user_id")
    private User user;

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name = "department_id")
    private Department department;

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name = "role_id")
    private Role role;

底线是,您可以在此处使用“多对多”,如本文所述 - >示例。但在我看来,如果你像上面那样映射链接表,你会省去很多麻烦。最后,电话是你的。


答案 2

推荐