Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

History tracking with JPA

I would like to implement history tracking/auditing for one of my model entities by using a new table to store a log with the user who made the change, the date, and whatever changed in each update/insert.

I'm using EclipseLink as my JPA provider, but I don't want to use its History Policy because the provider can change in the future. I can't either use Hibernate Envers for the same reason.

I looked into the auditing support which Spring Data provides, but it seems very basic and it only allows you to store the user who created or modified the entity and the dates, not the changes.

I also know about JPA's prePersist and preUpdate, but the specificacion says that:

In general, the lifecycle method of a portable application should not invoke EntityManager or Query operations, access other entity instances, or modify relationships within the same persistence context.

Is there a clean, easy and portable way to implement this?

like image 936
zootropo Avatar asked Nov 12 '22 17:11

zootropo


1 Answers

I have made that manually, with a separate EJB service, whose responsibility was to extract history data and save it as a MAP. It is very rudimentary in which only basic data and the IDs of relationships are saved, but that was enough for my case.

Basically I was inspecting the persistent properties with a bit of reflection.

like image 195
V G Avatar answered Nov 15 '22 13:11

V G