0

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.

dexBerlin
  • 437
  • 3
  • 9
  • 22
  • possible duplicate of [Why is my Spring @Autowired field null?](http://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null) – chrylis -cautiouslyoptimistic- Jul 08 '14 at 12:42
  • I've seen the other one, but I think my context is different. And the fix over there (similar to the one below) does not help out. – dexBerlin Jul 08 '14 at 12:53
  • The `@Configurable` approach is the one used by Spring Roo and the only reliable way to make sure that newly allocated objects get wired. Note that it requires AspectJ weaving and Spring Aspects. – chrylis -cautiouslyoptimistic- Jul 08 '14 at 12:58
  • As I wrote in the comments to geoand's answer, I try exactly this but somewhere sits a little buck trying to challenge me. – dexBerlin Jul 08 '14 at 13:00

1 Answers1

1

The reason what you are trying to do does not work, is that Entities are not managed by Spring, but by your JPA provider. There for, injection of dependencies will not occur.

In order to make dependency injection work on non Spring-managed classes, you need to use @Configurable along with a Java agent.

Check out this and this.

geoand
  • 60,071
  • 24
  • 172
  • 190
  • I added the @Configurable(preConstruction = true) option, added the three aspectj jars, and added spring-instrumentation to the IntelliJ run-configuration, but the repository still is null. – dexBerlin Jul 08 '14 at 11:32
  • Are you sure that the instrumentation is actually working? Is there anything relevant in the log? If instrumentation does work did you try replacing `@Resource` with `@Autowired`? – geoand Jul 08 '14 at 11:37
  • I do not see anything in my logs but ps gives me `/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/bin/java -D[Standalone] -server -Xdebug -Xrunjdwp:transport=dt_socket,address=127.0.0.1:60182,suspend=y,server=n -javaagent:/Users/dexBerlin/.m2/repository/org/springframework/spring-instrument/4.0.5.RELEASE/spring-instrument-4.0.5.RELEASE.jar [...]`, so I guess it should work? Yes, I tried both, `@Resource` and `@Autowired` – dexBerlin Jul 08 '14 at 11:43
  • The launching of the application seems correct. I can't say I have any other ideas about what's going on... – geoand Jul 08 '14 at 11:48
  • 1
    Anyway, thank you a lot for the effort you put here. – dexBerlin Jul 08 '14 at 11:51
  • @dexBerlin Are you compiling with `ajc` or weaving post-compile? – chrylis -cautiouslyoptimistic- Jul 08 '14 at 12:59
  • I enabled post-compiling in IntelliJ. I am very new to the AspectJ topic, but I see the IDE is writing "Weaving..." while making the build. – dexBerlin Jul 08 '14 at 13:58
  • May be injection can happen only after object is constructed?!! – Stalin Gino Mar 30 '22 at 12:11