History tracking with JPA

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

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?

回答1:

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.



转载请标明出处:History tracking with JPA
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!