Issue
I don't understand what kind of mysticism. This method located in class UserService:
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
User user = repository.getByEmail(email.toLowerCase());
if (user == null) {
throw new UsernameNotFoundException("User " + email + " isn't found");
}
AuthorizedUser authorizedUser = new AuthorizedUser(user); // 1
Objects.requireNonNull(authorizedUser); // 2
System.out.println(new AuthorizedUser(user) + " 1"); // 3
int userId = authorizedUser.getId(); // 4
return authorizedUser; // 5
}
In the lines which are numbered in comments, why does everything go smoothly in line 1-3, line 3 goes to the console:
UserTo{id=100000, name='VadimUserAdmin', email='[email protected]'} 1
And in line 4, an exception is thrown:
Caused by: java.lang.IllegalArgumentException: Entity must has id
at org.springframework.util.Assert.notNull(Assert.java:201)
at topjava.quest.HasId.id(HasId.java:14)
at topjava.quest.AuthorizedUser.getId(AuthorizedUser.java:22)
at topjava.quest.service.UserService.loadUserByUsername(UserService.java:64)
What should I do with this?
spring.security.version - 5.6.2
hibernate.version - 5.6.5.Final
class AuthorizedUser:
public class AuthorizedUser extends org.springframework.security.core.userdetails.User {
@Serial
private static final long serialVersionUID = 1L;
private UserTo userTo;
public AuthorizedUser(User user) {
super(user.getEmail(), user.getPassword(), true, true, true, true, user.getRoleSet());
setUserTo(Util.userAsTo(user));
}
public int getId() {
return userTo.id();
}
public void setUserTo(UserTo userTo) {
userTo.setPassword(null);
this.userTo = userTo;
}
@Override
public String toString() {
return userTo.toString();
}
}
class UserTo:
public class UserTo extends BaseTo implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@NotBlank
@Size(min = 2, max = 100)
@ApiModelProperty(example = "New name")
private final String name;
@Email
@NotBlank
@Size(max = 100)
@ApiModelProperty(example = "[email protected]")
private final String email;
@NotBlank
@Size(min = 6, max = 32)
@ApiModelProperty(example = "newmame123")
private String password;
@ConstructorProperties({"id", "name", "email", "password"})
public UserTo(Integer id, String name, String email, String password) {
super(id);
this.name = name;
this.email = email;
this.password = password;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "UserTo{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
'}';
}
}
class BaseTo:
public class BaseTo implements HasId {
@ApiModelProperty(hidden = true)
protected Integer id;
public BaseTo() {
}
public BaseTo(Integer id) {
this.id = id;
}
@Override
public Integer getId() {
return null;
}
@Override
public void setId(Integer id) {
this.id = id;
}
}
class HasId:
public interface HasId {
Integer getId();
void setId(Integer id);
default boolean isNew() {
return getId() == null;
}
default int id() {
Assert.notNull(getId(), "Entity must has id");
return getId();
}
}
What I do wrong?
Solution
When you call HasId.id
, you call method getId()
from BaseTo.class
, which returns null
. Change the method to
getId(){
return this.id;
}
Answered By - qwester
Answer Checked By - David Marino (JavaFixing Volunteer)