java spring boot中两个实体之间的多对多关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42394095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:35:41  来源:igfitidea点击:

many-to-many-relationship between two entities in spring boot

javamysqlspringjpaspring-boot

提问by Beytullah Güneyli

I have two Entities in my Spring-Boot Application:

我的 Spring-Boot 应用程序中有两个实体:

User.java

用户.java

@Entity
public class User {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      Long id;
      String firstname;
      String lastname;
      String username;
      String password;
}

and

Role.java

角色.java

Entity
@Table(name = "role")
public class Role {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      Long id;
      String name;
      String description;
}

for my MySql database

对于我的 MySql 数据库

I have excluded the getter and setter methods for this question.

我已经排除了这个问题的 getter 和 setter 方法。

I want to realise a Many-to-Many-Relationshipbetween both Entities. Every user should be able to assign multiple roles to himself

我想实现两个实体之间的多对多关系。每个用户都应该能够为自己分配多个角色

I already Created a mapping table for both tables in my database. It has the rows

我已经为我的数据库中的两个表创建了一个映射表。它有行

  • user_id
  • role_id.
  • 用户身份
  • 角色 ID。

I also created a new Entity UserRole.javawhich looks like this:

我还创建了一个新的实体UserRole.java,如下所示:

@Entity
@Table(name = "user_role")
public class UserRole implements Serializable{

    private User user;
    private Role role;

    @Id
    @ManyToOne
    @JoinColumn(name = "user_id")
    public User getuser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Id
    @ManyToOne
    @JoinColumn(name = "role_id")
    public Role getrole(){
        return role;
    }
    public void setRole(Role role){
        this.role = role;
    }

}

Now my question: is this construction correct? If yes, how do i add existing roles to an existing user and get the roles of this user in spring-boot?

现在我的问题是:这种结构正确吗?如果是,我如何将现有角色添加到现有用户并在 spring-boot 中获取该用户的角色?

采纳答案by Alex Efimov

You can find any tutorial connected with many-to-many relationship using Hibernate/Spring Data, example: Spring Data many-to-many

您可以使用 Hibernate/Spring Data 找到与多对多关系相关的任何教程,例如: Spring Data many-to-many

With your model it's simple to add the relationship mappings, like this:

使用您的模型,添加关系映射很简单,如下所示:

@Entity
public class Role {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private String description;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable
    private Set<User> users;
}

and this:

还有这个:

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String firstname;
    private String lastname;
    private String username;
    private String password;

    @ManyToMany(mappedBy = "users")
    private Set<Role> roles;
}