如何解决这个问题:创建bean时出错?

jgwigjjp  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(495)

我得到以下错误,我感到沮丧顺便说一句。请帮助。
这是我得到的错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository' defined in com.oetr.ticketsysback.ticketsys_back.repository.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.oetr.ticketsysback.ticketsys_back.entity.User com.oetr.ticketsysback.ticketsys_back.repository.UserRepository.findByUsername(java.lang.String)! Unable to locate Attribute  with the the given name [username] on this ManagedType [com.oetr.ticketsysback.ticketsys_back.entity.User]

这是我的用户实体:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "users")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "users_id")
    private int users_id;

    @Column(name = "users_firstname")
    private String users_firstname;

    @Column(name = "users_lastname")
    private String users_lastname;

    @Column(name = "users_username")
    private String users_username;

    @Column(name = "users_password")
    private String users_password;

    public User(String firstname, String name, String username, String password) {
        this.users_firstname = firstname;
        this.users_lastname = name;
        this.users_username = username;
        this.users_password = password;
    }

    public String getFirstname() {
        return users_firstname;
    }

    public String getLastname() {
        return users_lastname;
    }

    public String getUsername() {
        return users_username;
    }

    public String getPassword() {
        return users_password;
    }

    public int getId() {
        return users_id;
    }

}

这是我的 UserRepository :

public interface UserRepository extends JpaRepository<User, Integer> {

    User findByUsername(String username);

}

这是我的 UserController :

@RestController
@CrossOrigin
public class UserController {

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private AuthenticationManager authenticationManager;

    //@Autowired
    //private PasswordEncoder passwordEncoder;

    @Autowired
    private UserRepository userRepository;

    @PostMapping("/authenticate")
    public ResponseEntity<?> authUser(@RequestBody AuthRequest authRequest) throws Exception {

        try {
            authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(authRequest.getUsername(), authRequest.getPassword()));
        } catch (Exception ex) {
            throw new Exception("invalid username or password");
        }

        final String jwt = jwtUtil.generateToken(authRequest.getUsername());
        final long expiration = jwtUtil.extractExpiration(jwt).getTime();
        int userid = userRepository.findByUsername(authRequest.getUsername()).getId();

        return ResponseEntity.ok(new AuthenticationResponse(jwt, expiration, userid));

    }
}

我怎样才能解决这个问题?如果你需要更多,请告诉我。

wsewodh2

wsewodh21#

您应该删除字段的前缀:“users\”,因为在spring数据中,下划线符号用于在存储库方法名称中的嵌套实体上建立链接。例如,请参见:spring data,find by property of a nested object

相关问题