JPA实体结构

时间:2018-03-11 16:58:23

标签: java jpa spring-boot repository

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

用户

@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:   空

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

1 个答案:

答案 0 :(得分:0)

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

此外,不是在getter中添加@ManyToMany注释,而是在声明本身时添加它。在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是第三个表格。