Reputation: 35
In short, I try make authorization with Spring security. But I get error
Caused by: java.lang.IllegalArgumentException: Not a managed type: class java.lang.Long
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleRepository': Invocation of init method failed;
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDetailsServiceImpl': Unsatisfied dependency expressed through field 'roleRepository';
Interface RoleRepository implements methods from JpaRepository.
@Repository
public interface RoleRepository extends JpaRepository<Long, Roles>{
@Query("select r from roles r where id = 1")
public List<Roles> getRole(long userId);
}
It's associated with UserDetailsServiceImpl class, which implements loadUserByUsername method. In this method I getting user data (role, username, password).
I think that problem in entities classes (Roles, Users, UserRole), there I use OneToMany binding: UserRole consists user_id and role_id fields, which associated with Users and Roles tables repsectively. I don't understand, where exactly is the error.
UserRole:
@Entity
@Table(name = "user_role")
public class UserRole {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id", nullable = false)
private Users users;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "role_id", nullable = false)
private Roles roles;
// getters, setters
}
Roles:
@Entity
@Table(name = "roles")
public class Roles {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "name")
private String name;
@OneToMany(mappedBy = "roles")
private Set<UserRole> roles;
// getters, setters
}
Users:
@Entity
@Table(name = "users")
public class Users {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long uid;
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
@Column(name = "enabled")
private boolean enabled;
@OneToMany(mappedBy = "users")
private Set<UserRole> users;
// getters, setters
}
Upvotes: 0
Views: 5304
Reputation: 44707
You have RoleRepository
class as below
@Repository
public interface RoleRepository extends JpaRepository<Long, Roles>{
@Query("select r from roles r where id = 1")
public List<Roles> getRole(long userId);
}
which needs to be changed as below because the the spring managed entity type Roles
need to be the first argument.
@Repository
public interface RoleRepository extends JpaRepository<Roles, Long>{
@Query("select r from roles r where id = 1")
public List<Roles> getRole(long userId);
}
Upvotes: 9