Home > Software engineering >  Why JpaRepository<User, Long> returns null value
Why JpaRepository<User, Long> returns null value

Time:05-03

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?

CodePudding user response:

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

CodePudding user response:

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

  • Related