如何使用Spring Data JPA搜索非唯一索引?

时间:2019-01-14 12:31:13

标签: java spring-boot jpa spring-security spring-data-jpa

我正在学习带有用户角色的Spring Security的Spring Data JPA,我想知道如何使用Spring Data JPA搜索非唯一索引。例如,我有3个MySQL表,这是联接表:

enter image description here

我想查找特定用户的所有角色。因此,我想通过user_id在user_role表中进行搜索,但这不是唯一键。

用户实体:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="user")
public class AppUser {

    @Id
    @GeneratedValue
    @Column(name="user_id")
    private long id;

    private String username;
    private String password;
    private boolean enabled;

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public boolean isEnabled() {
        return enabled;
    }
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

}

角色实体:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="role")
public class AppRole {

    @Id
    @GeneratedValue
    @Column(name="role_id")
    private long id;

    private String name;

    public long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

User_Role实体:

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

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

    @Id
    @GeneratedValue
    private long id;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="user_id")
    private AppUser appUser;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="role_id")
    private AppRole appRole;

    public long getId() {
        return id;
    }

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

    public AppUser getAppUser() {
        return appUser;
    }

    public void setAppUser(AppUser appUser) {
        this.appUser = appUser;
    }

    public AppRole getAppRole() {
        return appRole;
    }

    public void setAppRole(AppRole appRole) {
        this.appRole = appRole;
    }
}

我想为Spring Security创建UserDetails,因为我想使用用户角色。我需要查找特定用户的所有角色。我已经创建了扩展JpaRepository的存储库接口,但是我不知道如何搜索user_id。我可以使用Spring Data JPA搜索此user_id吗?诸如userRoleRepository.findByUserId(user.getId)之类的东西。任何反馈将不胜感激!

1 个答案:

答案 0 :(得分:1)

在我看来,您的实体存在问题,即使您需要联接表,也不需要UserRole实体。

JPA允许您使用@ManyToMany@JoinTable注释:

pubic class User {
  ...
  @ManyToMany
  @JoinTable(name="USER_ROLE")
  private List<Role> roles;
  ...
}

这样,您可以先使用用户存储库轻松获取用户,然后再使用与之关联的角色。

User user = userRepository.findOne(userId);
List<Role> roles = user.getRoles();