列出最近保存的和新的之间的实体类字段级增量的最简单方法 [英] Easiest way to list entity class field-level deltas between last saved and new

查看:241
本文介绍了列出最近保存的和新的之间的实体类字段级增量的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,我必须检测并保存实体类的上次保存的更改与新更改之间的字段级增量,可以通过 JSF 进行修改前端。 IOW,表单,实体类和数据库表可能具有X个字段,并且每次保存时,都可以更改0到X之间的任何值。我需要在每个保存上找到所有有更改的记录,并将它们记录在某处以进行审核。

I have a requirement by which I have to detect and save field-level deltas between the last saved and new changes of an entity class, which is modified through a JSF front end. IOW, the form, entity class, and the DB table may have X number of fields and on every save, anywhere between 0 and X can be changed. I need to find on every save all the ones that have changes and log them somewhere for audit purposes.

起初,我想到了修改每个设置器的方法:

At first I thought of modifying each setter:

@Transient
private Set<String> changes;

...

public void setSomeField(String _newval) {

    if(!_newval.equals(this.someField)){
        this.changes.add("SOME_FIELD");
    }

    this.someField = _newval;
}

这将要求在每个设置器中实现此非优雅增量检测逻辑我不喜欢有没有一种简单的方法可以使用某些 JPA 函数将新状态与持久状态进行区分,该函数会列出所有字段更改,然后可以转储到我的审核日志存储?

which would require this non-elegant delta detection logic to be implemented in each setter and I don't like it. Is there an easy way to diff the new state from the persisted state using some JPA function that would give me a listing of all the fields changes and that I can then dump into my audit log storage?

推荐答案

听起来像完美的拦截器,加上注释。创建一个,注释实体(类级别会很棒,但我认为方法级别(设置者)可以接受),所有工作都是自动完成的。何时记录什么以及何时记录结果取决于您,但这可以在entitylistener中进行。

Sounds like a perfect fit an interceptor in combination with an annotation. Create one, annotate the entity (classlevel would be great, but method level (setters) would be acceptible I think) and all work is done automagically. What and when you do log the results is up to you, but that could be in an entitylistener.

但是为什么不使用 Envers ,它确实可以完成所有操作此(与afaik jpa实施无关)

But why not use Envers it does excately all this (afaik jpa implementation independent)

这篇关于列出最近保存的和新的之间的实体类字段级增量的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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