I have an entity like this:
@EntityListeners({MyEntityListener.class})
public class MyEntity {
@OneToMany(mappedBy = "myentity", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<MyDependentEntity> dependents;
//other attributes that are mapped to columns in MyEntity table
}
And the dependent entity:
public class MyDependentEntity {
@ManyToOne(optional = false, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
@JoinColumn(name = "myentity_id")
private MyEntity myEntity;
}
And MyEntityListener:
public class MyEntityListener {
@PostPersist
public void postPersist(MyEntity entity) {
logStuff(entity);
}
}
And this code that starts it:
MyDependentEntity dependent = new MyDependentEntity();
dependent.set...(...);
dependent.set...(...);
MyEntity entity = new MyEntity();
entity.set...(...);
entity.set...(...);
entity.set...(...);
entity.setDependents(Collections.singleton(dependent))
dependent.setEntity(entity);
entityManager.merge(entity);
It works and the entity listener is triggered. The problem is: the dependents attributes is null (all others are correctly populated).
The database has the correct data (entity and the corresponding dependents). Debugging I can see that the entity persisted has the dependent in the attribute. But as it enters in the entity listener the dependent attribute is null.
Is there any way that I can have the dependents attribute populated in the entity listener? I need it so I can process the Entity which was just saved.