Получение ошибки «Неуправляемый тип: класс java .lang.Long» - PullRequest
0 голосов
/ 02 августа 2020

Короче пытаюсь авторизоваться с помощью Spring security. Но я получаю ошибку

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';

Интерфейс RoleRepository реализует методы из JpaRepository.

https://github.com/TimurShubin/cbrparser/blob/master/src/main/java/com/cbr/converter/repositories/RoleRepository.java

@Repository
public interface RoleRepository extends JpaRepository<Long, Roles>{

    @Query("select r from roles r where id = 1")
    public List<Roles> getRole(long userId);
    
}

Он связан с классом UserDetailsServiceImpl, который реализует метод loadUserByUsername. В этом методе я получаю данные пользователя (роль, имя пользователя, пароль).

Я думаю, что проблема в классах сущностей (Roles, Users, UserRole), там я использую привязку OneToMany: UserRole состоит из полей user_id и role_id, которые связанные с таблицами пользователей и ролей. Не понимаю, где именно ошибка.

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

}

Роли:

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

Пользователи:

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

1 Ответ

2 голосов
/ 02 августа 2020

У вас есть класс RoleRepository, как показано ниже

@Repository
public interface RoleRepository extends JpaRepository<Long, Roles>{

    @Query("select r from roles r where id = 1")
    public List<Roles> getRole(long userId);
    
}

, который необходимо изменить, как показано ниже, потому что тип управляемого объекта Spring Roles должен быть первым аргументом.

@Repository
public interface RoleRepository extends JpaRepository<Roles, Long>{

    @Query("select r from roles r where id = 1")
    public List<Roles> getRole(long userId);

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...