【问题标题】:JPA Entity structureJPA 实体结构
【发布时间】:2018-08-19 17:27:48
【问题描述】:

我有一个登录页面,将从数据库中检索数据

用户

@Setter
@Getter
@Entity
@Table(name = "USER_DETAILS")
public class User implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "USER_ID")
    private Long id;

    @Column(name = "USER_NAME")
    private String userName;

    @Column(name = "USER_PASSWORD")
    private String userPassword;

    @Transient
    private Set<Role> roles;


    @ManyToMany
    @JoinTable(name = "USER_ROLE", joinColumns = @JoinColumn(name = "USER_ID"), inverseJoinColumns = @JoinColumn(name = "ROLE_ID"))
    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

}

角色

@Setter
@Getter
@Entity
@Table(name = "USER_ROLE")
public class Role implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ROLE_ID")
    private Long id;

    @Column(name = "USER_NAME")
    private String userName;


    @Column(name = "ROLE_NAME")
    private String roles;

    @Transient
    private Set<User>users;


    @ManyToMany(mappedBy = "roles")
    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }
}

UserDetailServiceImpl

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserRepository userRepository;

    @Override
    @Transactional(readOnly = true)
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userRepository.findByUsername(username);
        System.out.println("Name "+user.getUserName());
        System.out.println("role is  "+user.getRoles());
        if(null == user) {
            throw new UsernameNotFoundException("No user present with username: " + username);
        }
        Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
        for (Role role : user.getRoles()){
            grantedAuthorities.add(new SimpleGrantedAuthority(role.getUserName()));
        }

        return new org.springframework.security.core.userdetails.User(user.getUserName(), user.getUserPassword(), grantedAuthorities);
    }

}

表格

角色

ROLE_ID | USER_NAME | ROLE_NAME
1             John     Admin

用户

  USER_ID | USER_NAME  | USER_PASSWORD | USER_ROLE
      1         John            pass       Admin

输出

姓名 John 角色为空 2018-03-12 00:52:06.362 ERROR 12563 --- [nio-8088-exec-8] w.a.UsernamePasswordAuthenticationFilter : An 尝试对用户进行身份验证时发生内部错误。

org.springframework.security.authentication.InternalAuthenticationServiceException: 空

我无法获取角色值。数据库结构有什么问题?

【问题讨论】:

    标签: java jpa spring-boot repository


    【解决方案1】:

    要使@ManyToMany 工作,您需要 3 个表而不是 2 个。两个父表通常通过包含两个外键的第三个表链接。所以这里应该有第三个表,其中有role_iduser_id

    另外,不要将@ManyToMany 注释添加到getter,而是尝试在声明本身时添加它。 User 类中的类似内容,

    @ManyToMany(cascade = { 
    CascadeType.PERSIST, 
    CascadeType.MERGE
    })
    @JoinTable(name = "user_role",
        joinColumns = @JoinColumn(name = "user_id"),
        inverseJoinColumns = @JoinColumn(name = "role_id")
    )
    private Set<Role> roles = new HashSet<>();
    

    Role类中的相同修改:

    @ManyToMany(mappedBy = "roles")
    private Set<User> users = new HashSet<>();
    

    这里,user_role 是第三个表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-07
      • 2023-03-24
      • 1970-01-01
      • 2020-07-26
      • 2014-08-30
      • 1970-01-01
      • 2021-06-18
      • 2020-12-22
      相关资源
      最近更新 更多