I have two entities with relation one to many. Parent can have several Child entity instances. I added a field to the parent that stores the date of children modifications(childrenLastModifiedDate). To maintain that, I added method:
@PrePersist
@PreUpdate
@PreRemove
private void handle() {
parent.setChildrenLastModifiedDate(now());
}
Here is the problem. It's not always invoke when the child is saved. Locally(mac os), it works as expected, all three types of changes are tracked and saved to the parent entity. However, on the server(linux) it only works for:
- @PrePersist
- @PreRemove
Even though, PreUpdate is invoked, the changes are not saved. I have even tried to add a direct repository call to save the parent. The result is the same. Nothing is saved on update, but saved on remove or persist. I tried to make this change in additional transaction and it worked, but it's too resource consuming. Also, I can see that someone had very similar experience:
JPA/Hibernate preUpdate doesn't update parent object
However, there is nothing on how to handle the problem itself. The question is: is there a way to guarantee that using of this annotations will always work and perform additional updates of dependent entities? If it's not, what is the best way to handle such logic? The update is required on each change to children. Even if it is saved by cascade.