Is it possible to update some column before every flush to database? I have modifiedOn and modifiedBy columns and want to update them on every DB update, similar to DB trigger. Is it possible with JPA?
Is it possible to update some column before every flush to database? I have
Share
Disclaimer: The following works only with Hibernate / JPA.
You can update the
modifiedOnproperty like this with JPA:As for the
modifiedBy, it is a little bit trickier since the JPA spec discourages references to other entities in the lifecycle callback methods. Furthermore, you would need some knowledge of the current user, which probably belongs to the service layer.You could use an
EntityListenerlike this (however, this still uses the callback methods)An in the EntityListener: