如何联接两个表并将其用作jpa存储库

时间:2018-08-09 07:32:59

标签: java mysql spring spring-data-jpa

我的英语说得不太好,所以我使用了translate.google))。

我在MySQL中有数据库:

enter image description here

和我的实体:

Role.java

 @Entity
 public class User {

    private Integer id;
    private String login;
    private String email;
    private String password;

    @Id
    @Column(name = "id", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
       this.id = id;
    }

    @Basic
    @Column(name = "login", nullable = false, length = 255)
    public String getLogin() {
       return login;
    }

    public void setLogin(String login) {
       this.login = login;
    }

    @Basic
    @Column(name = "email", nullable = false, length = 50)
    public String getEmail() {
       return email;
    }

    public void setEmail(String email) {
       this.email = email;
    }

   @Basic
   @Column(name = "password", nullable = true, length = 255)
   public String getPassword() {
       return password;
   }

   public void setPassword(String password) {
       this.password = password;
   }

   @Override
   public boolean equals(Object o) {
       if (this == o) return true;
       if (o == null || getClass() != o.getClass()) return false;
       User user = (User) o;
       return Objects.equals(id, user.id) &&
            Objects.equals(login, user.login) &&
            Objects.equals(email, user.email) &&
            Objects.equals(password, user.password);
   }

   @Override
   public int hashCode() {

      return Objects.hash(id, login, email, password);
   }

   public User() {
   }

   public User(String login, String email, String password) {
       this.login = login;
       this.email = email;
       this.password = password;
   }
}

User.java

@Entity
@Table(name = "user_role", schema = "liverpool_site", catalog = "")
public class UserRole {
    private Integer id;
    private Integer userId;
    private Integer roleId;

    @Id
    @Column(name = "id", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getId() {
       return id;
    }

    public void setId(Integer id) {
       this.id = id;
    }

    @Basic
    @Column(name = "user_id", nullable = false)
    public Integer getUserId() {
       return userId;
    }

    public void setUserId(Integer userId) {
       this.userId = userId;
    }

    @Basic
    @Column(name = "role_id", nullable = false)
    public Integer getRoleId() {
       return roleId;
    }

    public void setRoleId(Integer roleId) {
        this.roleId = roleId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        UserRole userRole = (UserRole) o;
        return Objects.equals(id, userRole.id) &&
                Objects.equals(userId, userRole.userId) &&
                Objects.equals(roleId, userRole.roleId);
    }

    @Override
    public int hashCode() {

          return Objects.hash(id, userId, roleId);
    }

    public UserRole() {
     }

     public UserRole(Integer userId, Integer roleId) {
         this.userId = userId;
         this.roleId = roleId;
    }
}

UserRole.java

public class UserRoleJpa{
    private Integer idUser;
    private String login;
    private String email;
    private String roleName;

    //getter and setter methods
}


public interface UserRoleJpa extends JpaRepository<UserRoleJpa, String> {
      //for example
      List<UserRoleJpa> findAll();
}

好的,我的问题是

现在这些表是如何连接的,并且可以用作jpa存储库,如下所示:

Tools > Android > AVD Manager

对于任何建议和实现方法的建议,我将感到非常高兴。

如果可能的话,可以使用Spring Data JPA))

1 个答案:

答案 0 :(得分:0)

我认为最好定义两个条目User和Role,然后使用ManyToMany批注进行映射,这将创建表user_role,其中将包含用户的ID和角色

您只需要将其添加到您的用户实体

@ManyToMany
@JoinTable(name="USERS_ROLES")
private Collection<Role>roles;

而且不需要userRole实体