javaspringhibernatejpaaudit-trail

Field based auditing with Spring


In my actual project (Spring Boot 2 with JPA on Hibernate) I need to implement a field based audit trail. If the user changes any field then he/she needs to enter a reason. If multiple fields of an object are changed then each change must have a reason text separately.

There are 2 requirement on the review side:

  1. If I select a field I have to list all of the changes with the reason.
  2. If I select the main (built from multiple other objects) business object I have to list all of the changes.

I don't need to restore any old state.

I have already used Hibernate Envers, JaVer library and JPA auditing feature in the past for simple auditing where changes was handled on object level but not on field level. However in the actual case I don't know in which direction should I dig deeper.


Solution

  • Hibernate envrs enables field based auditing. In addition you can specify a custom revision entity where you can store extra information with each revision like user and reason. JPA auditing is not handling your case. It just enables you to use operation callbacks e.g. @PrePersist or @PostUpdate. JaVer may be an alternative to Hibernate Envrs, but as you already use Hibernate it will be much easier to integrate Envrs as a tool of the same family