繁体   English   中英

引起:java.lang.IllegalArgumentException: Entity must have id

[英]Caused by: java.lang.IllegalArgumentException: Entity must has id

搞不懂什么玄学。 此方法位于 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");
        }
1.      AuthorizedUser authorizedUser = new AuthorizedUser(user);
2.      Objects.requireNonNull(authorizedUser);
3.      System.out.println(new AuthorizedUser(user) + " 1");
4.      int userId = authorizedUser.getId();
5.      return authorizedUser;
    }

为什么go在第1-3行,第3行顺利进入控制台:

UserTo{id=100000, name='VadimUserAdmin', email='vadim@gmail.com'} 1

在第 4 行,抛出异常:

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)

我该怎么办?

spring.security.version - 5.6.2

hibernate.version - 5.6.5.Final

class 授权用户:

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 用户至:

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 = "newmame@gmail.com")
    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 底座至:

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();
    }
}

我做错了什么?

当您调用 HasId.id 时,您从 BaseTo.class 调用方法 getId(),而不是返回 null,更改方法 getId(){ return this.id;}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM