使用JPA进行历史跟踪 [英] History tracking with JPA

查看:164
本文介绍了使用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.

我使用EclipseLink作为我的JPA提供程序,但我不想使用它的历史政策,因为提供商将来可能会发生变化。出于同样的原因,我不能使用 Hibernate Envers

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.

我查看了审核支持Spring Data提供的,但它看起来非常基本,它只允许您存储创建或修改实体的用户和日期,而不是更改。

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.

我也知道JPA的 prePersist preUpdate ,但具体说明了:

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


通常,可移植应用程序的生命周期方法不应该
调用EntityManager或Query操作,访问其他实体
实例,或修改相同持久性
上下文中的关系。

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?

推荐答案

我已经使用单独的EJB服务手动完成了这项工作,该服务的职责是提取历史数据并将其另存为MAP。这是非常基本的,只保存了基本数据和关系ID,但这对我的情况来说已经足够了。

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.

这篇关于使用JPA进行历史跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆