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

问题描述 投票:1回答:1

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

enter image description here

我想找到特定用户的所有角色。所以我想按用户ID在用户角色表中搜索,这不是唯一的密钥。

用户实体:

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)之类的东西。任何反馈将是apreciated!

java spring-boot jpa spring-security spring-data-jpa
1个回答
1
投票

在我看来,你的实体有问题,你不需要一个qazxsw poi实体,即使你需要一个连接表。

JPA允许您使用UserRole@ManyToMany注释执行此操作:

@JoinTable

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

pubic class User {
  ...
  @ManyToMany
  @JoinTable(name="USER_ROLE")
  private List<Role> roles;
  ...
}
© www.soinside.com 2019 - 2024. All rights reserved.