Skip to content
Advertisement

How to solve this Problem: Error creating bean?

I am getting the following error and I am getting frustrated btw. Please help.

This is the Error I am getting:

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]

This is my User Entity:

@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;
    }

}

This is my UserRepository:

public interface UserRepository extends JpaRepository<User, Integer> {

    User findByUsername(String username);

}

And this is my 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));

    }
}

How can I solve this problem? Let me know if you need more.

Advertisement

Answer

You should remove prefix: “users_” for fields, because in spring-data underscore symbol is used for making link on nested entities in repository method names. For example see: Spring data, find by property of a nested object

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement