英文:
Getting an error "Not a managed type: class java.lang.Long"
问题
以下是您提供的内容的翻译:
简而言之,我尝试使用Spring Security进行授权。但是我遇到了错误:
由:java.lang.IllegalArgumentException引起:不是托管类型:class java.lang.Long
由:org.springframework.beans.factory.BeanCreationException引起:无法创建名为'roleRepository'的bean:init方法的调用失败;
由:org.springframework.beans.factory.UnsatisfiedDependencyException引起:无法创建名为'userDetailsServiceImpl'的bean:通过字段'roleRepository'表达的不满足的依赖项;
接口RoleRepository实现了JpaRepository中的方法。
@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字段,分别与Users和Roles表关联。我不明白错误究竟在哪里。
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;
// 获取器、设置器
}
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;
// 获取器、设置器
}
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;
// 获取器、设置器
}
英文:
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
}
答案1
得分: 6
以下是翻译好的部分:
原始代码部分:
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 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);
}
英文:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论