'Why JpaRepository<User, Long> returns null value

I have a Spring Boot project, in which I have problem with JpaRepository<User,Long>.

I defined following interface

public interface UserDAO extends JpaRepository<User, Long> {
    User findByEmail(String email);
}

which was implemented in UserServiceImpl

@Override
public User findByEmail(String email) {
    return userDAO.findByEmail(email);
}

and I need it in UserDetailsServiceImpl but next line

User user = userDAO.findByEmail(email);

returns null.

My model classes:

User

@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;

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

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

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

    @ManyToMany
    @JoinTable(name = "user_roles",
            joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "role_id"))
    private Set<Role> roles;

    @Transient
    private String confirmPassword;
    //getters-setters

}

Role

@Entity
@Table(name = "roles")
public class Role implements GrantedAuthority {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

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

    @ManyToMany(mappedBy = "roles")
    private Set<User> users;

What can I do?



Solution 1:[1]

Try to use the annotations on the getter, not the fields.

Solution 2:[2]

You need to follow JavaBean specification for entity(you need to have default contrucor and getters and setters in User and Role)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Grim
Solution 2 EffectiveIvana