I was wondering is there any easy way to implement the tracking of changes in the entities ? There is Envers from Hibernate to have an audit, but as far as I understood it's Hibernate oriented. I am thinking if there is something inside JPA, or a solution which does not get outside of the spec. If there isn't made any, could somebody maybe throw me an idea how to get started with this kind of thing. One idea which comes to me is to create an entity for example :
class Change {
String className;
long id;
String fieldName;
String fieldValue;
Date dateOfChange;
}
Which would contain the changed properties. This solution seems to be quite efficient in terms of storage place, but it could be more difficult to handle relations between entities which are tracked ( did not figured it out yet ).
I greatly appreciate any input in this topic,
Kind regards, P.
You can use the @PrePersist
Annotation to create an Entry and store it to the Database.
class Entity{
@PrePersist
public void logChanges(){
Change c = new Change()
c.setEverythingXouLikeToLog();
entityManager.persist(c);
// don't forget ExceptionHandling
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With