获得一个错误信息:”不是受管理的类型:class java.lang.Long”

huangapple go评论81阅读模式
英文:

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中的方法。

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字段,分别与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 &#39;roleRepository&#39;: Invocation of init method failed;

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name &#39;userDetailsServiceImpl&#39;: Unsatisfied dependency expressed through field &#39;roleRepository&#39;;

Interface RoleRepository implements methods from JpaRepository.

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

@Repository
public interface RoleRepository extends JpaRepository&lt;Long, Roles&gt;{

	@Query(&quot;select r from roles r where id = 1&quot;)
	public List&lt;Roles&gt; 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 = &quot;user_role&quot;)
public class UserRole {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private long id;
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = &quot;user_id&quot;, nullable = false)
	private Users users;
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = &quot;role_id&quot;, nullable = false)
	private Roles roles;

    // getters, setters

}

Roles:

@Entity
@Table(name = &quot;roles&quot;)
public class Roles {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private long id;
	
	@Column(name = &quot;name&quot;)
	private String name;
	
	@OneToMany(mappedBy = &quot;roles&quot;)
	private Set&lt;UserRole&gt; roles;

    // getters, setters
}

Users:

@Entity
@Table(name = &quot;users&quot;)
public class Users {
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private long uid;
	
	@Column(name = &quot;username&quot;)
	private String username;
	
	@Column(name = &quot;password&quot;)
	private String password;

	@Column(name = &quot;enabled&quot;)
	private boolean enabled;

	@OneToMany(mappedBy = &quot;users&quot;)
	private Set&lt;UserRole&gt; users;

    // getters, setters
}

答案1

得分: 6

以下是翻译好的部分:

原始代码部分:

You have `RoleRepository` class as below 

    @Repository
    public interface RoleRepository extends JpaRepository&lt;Long, Roles&gt;{
    
    	@Query(&quot;select r from roles r where id = 1&quot;)
    	public List&lt;Roles&gt; 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&lt;Roles, Long&gt;{
    
    	@Query(&quot;select r from roles r where id = 1&quot;)
    	public List&lt;Roles&gt; getRole(long userId);
	
    }
英文:

You have RoleRepository class as below

@Repository
public interface RoleRepository extends JpaRepository&lt;Long, Roles&gt;{

	@Query(&quot;select r from roles r where id = 1&quot;)
	public List&lt;Roles&gt; 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&lt;Roles, Long&gt;{

	@Query(&quot;select r from roles r where id = 1&quot;)
	public List&lt;Roles&gt; getRole(long userId);

}

huangapple
  • 本文由 发表于 2020年8月2日 17:13:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63214286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定