При запуске сервера я застреваю.
Данные
Видимо дело в моделях, но не понятно, что.
Подключение к базе через JPA.
Пользователь
@Entity
@Table (name = "User")
public class User implements UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "surname")
private String surname;
@Column(name = "password")
private String password;
@ManyToMany(fetch = FetchType.EAGER, /*cascade = CascadeType.ALL,*/
targetEntity = Role.class)
@JoinTable(name = "user_role",
joinColumns = {@JoinColumn(name = "user_id")},
inverseJoinColumns = {@JoinColumn(name = "role_id")})
private Set<Role> roles;
public User() {
}
public User(String name, String surname) {
this.name = name;
this.surname = surname;
}
public User(String name, String surname, String password, Set<Role> roles) {
this.name = name;
this.surname = surname;
this.password = password;
this.roles = roles;
}
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;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public void setPassword(String password) {
this.password = password;
}
public String getPass() {
return password;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", surname='" + surname + '\'' +
//", role='" + roles.toString() + '\'' +
'}';
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Set<Role> roleList = new HashSet<>();
roleList.addAll(roles);
return roleList;
}
@Override
public String getPassword() {
return password;
}
@Override
public String getUsername() {
return name;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
Роль
@Entity
@Table(name = "Role")
public class Role implements GrantedAuthority {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "roleName", length = 20, nullable = false)
private String roleName;
//private String role;
@ManyToMany(fetch = FetchType.EAGER, /*mappedBy = "roles",*/ targetEntity =
User.class)
@JoinTable(name = "user_role",
joinColumns = {@JoinColumn(name = "role_id")},
inverseJoinColumns = {@JoinColumn(name = "user_id")})
private List<User> users;
public Role() {
}
public Role(String roleName) {
this.roleName = roleName;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Collection<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
@Override
public String toString() {
return "Role with id " + id + " and name " + roleName;
}
public String getAuthority() {
return getRoleName();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Role role = (Role) o;
return Objects.equals(roleName, role.roleName);
}
@Override
public int hashCode() {
return Objects.hash(roleName);
}
}
Я думал, что это каскад и FetchType. Но все кажется правильным.
Я пытался использовать toString для связи пользователя с ролью и наоборот. Также без результата.
Спасибо!