休眠审核字段更改的日志 [英] Hibernate audit log of fields' change

查看:58
本文介绍了休眠审核字段更改的日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将实体的更改记录到日志文件中?考虑我有这样的 Person .

  import org.hibernate.envers.Audited;导入javax.persistence.Entity;导入javax.persistence.Id;导入javax.persistence.GeneratedValue;导入javax.persistence.Column;@实体@审核公共班级的人{@ID@GeneratedValue私人int ID;私有字符串名称;私有字符串姓;//在此处添加getter,setter,构造函数,equals和hashCode} 

以及更改现有 Person

的代码

  Person p1 = new Person("name-1","surname-1");personRepository.save(p1);人p2 = personRepository.findOne(1L);p2.setName("new-name");personRepository.save(p2); 

我怎么有

  • 旧实体
  • 新实体
  • 字段列表已更改(类似 Diffable 的结果

在我的日志文件中?我知道 envars 可以将更改存储在db中,并让我以后使用提取它们AuditReader ,但我想将更改存储在Json文件中,以将其发送给第三方应用程序(例如Elastic).

解决方案

您可以通过实现 org.hibernate.EmptyInterceptor 来编写自定义拦截器.它具有使用实体的旧快照和新快照更新/插入/删除的回调.

有关更多详细信息,请参见这篇文章

How can I log the changes of the entity into log files? Consider I have Person like this.

import org.hibernate.envers.Audited;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.Column;

@Entity
@Audited
public class Person {
    @Id
    @GeneratedValue
    private int id;

    private String name;
    private String surname;
// add getters, setters, constructors, equals and hashCode here
}

and a code of changing existing Person

Person p1 = new Person("name-1", "surname-1");
personRepository.save(p1);
Person p2 = personRepository.findOne(1L);
p2.setName("new-name");
personRepository.save(p2);

How can I have

  • old entity
  • new entity
  • list of fields changed (some thing like Diffable's result)

In my log file? I know that envars can store changes in db and let me extract them later with AuditReader but I like to store changes in Json file to send them to third party applications (like Elastic).

解决方案

You could write a custom interceptor by implementing org.hibernate.EmptyInterceptor. This has callbacks to update/insert/delete with old and new snapshots of entities.

Refer this article for more details

这篇关于休眠审核字段更改的日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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