We have two Spring Data JPA Entities (parent,child), the count of children with a field set to a particular value, influences the value of a parent's record in a @Transient
property set during @PostLoad
Parent:
@Entity
public class Parent {
@Transient
private boolean status = false;
@OneToMany
@Where("STATUS = true")
private Set<Children> childrens;
@PostLoad
public void postload(){
if(childrens.size() > 0) this.status = true;
}
....
}
Children:
@Entity
@EntityListeners({ ParentListener.class })
public class children {
private Boolean status = false;
@ManyToOne
private Parent parent;
}
In my controller / service classes (which are NOT annotated as @Transactional
, I come along and update the status value of a Children
record:
@Service
public class ChildrenService {
...
public void doStuff(Children child) {
child.status = true;
childRepository.save(child);
}
}
Now the ParentListener
kicks in, and I want to log when the parent's status value changes.
class ParentListener {
@PostUpdate // AFTER Children record updated
public void childPostPersist(Children child) {
AutowireHelper.autowire(this);
// here child.parent.status == false (original value)
// but I have set this child record equal to true, and
// have triggered the `save` method, but assuming I am still
// in the session transaction or flush phase, the
// parent related record's status is not updated?
System.out.println(child.parent.status); // prints false
Parent currentParent = parentRepository.getOne(child.parent.getId());
System.out.println(currentParent.status); // prints false
}
}
What am I misunderstanding about @Transactional
, @Postload
and transactions/sessions and EntityListeners
?
PS. AutowireHelper is reference from here