我尝试将存储库注入实体,因为在特殊情况下我需要从名称创建实体(这是我从JSON到jackson的结果)。这个实体很简单:
+----+------+
| id | name |
+----+------+
Role实体应该注入RoleRepository。我目前正在尝试:
@Entity
@Component
@Table(name = "roles" )
public class Role {
@Resource
@Transient
private RoleRepository roleRepository;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer roleId;
@Column(name = "name")
private String name;
@ManyToMany(mappedBy = "roles")
List<SystemUser> systemUsers;
public Role() { }
@JsonCreator
public Role(String roleString) {
Role role = roleRepository.findByName(roleString).get(0); /*** HERE ***/
setRoleId(role.getRoleId());
setName(role.getName());
}
public Role(Integer id, String name) {
this.roleId = id;
this.name = name;
}
public Integer getRoleId() {
return roleId;
}
public void setRoleId(Integer roleId) {
this.roleId = roleId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
if(getName() != null) {
return WordUtils.capitalize(getName().toLowerCase());
} else {
return "";
}
}
}
当前问题:在标有&#34; HERE&#34;的行上,roleRepository为空。所以我得到了一个NullPointer并且没有获取我想要的Role实体。