I try to inject a repository into an entity, because in a special case I need to create the entity from name (this is what I get from JSON through jackson). This entity is quite simple:
+----+------+
| id | name |
+----+------+
The Role entity should have the RoleRepository injected. I currently try:
@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 "";
}
}
}
Current issue: roleRepository is null at the line I marked with "HERE". So I get a NullPointer and do not fetch the Role entity I wanted to have.