简体   繁体   English

如何在双向中通过hibernate envers审核一对一关系?

[英]How to Audit one-to-one relation through hibernate envers in bidirectional?

I am using hibernate envers for auditing. 我正在使用hibernate envers进行审计。

I have two entity classes, A and B. There is a one-to-one relationship between them. 我有两个实体类,A和B.它们之间有一对一的关系。 So this creates two audit tables A_aud and B_aud. 因此,这会创建两个审计表A_aud和B_aud。 Creation/Updates for both is done through one screen. 两者的创建/更新都通过一个屏幕完成。

So my requirement is that whenever there is a change in any table of both (in any field of them), I need an audit entry in both tables. 所以我的要求是,只要两个表中的任何表(在它们的任何字段中)发生变化,我都需要在两个表中都有一个审计条目。

How can I achieve this? 我怎样才能做到这一点?

And this how I defined the mapping on both sides 这就是我如何定义双方的映射

    public class A implements Serializable {
           private B b;

           @OneToOne(mappedBy = "a", cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
           public B getB() {
             return b;
           }

           public void setB(B b) {
             this.b = b;
           }
}

public class B implements Serializable{
    private A a;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "A_ID", referencedColumnName = "id",nullable = false)
    public A getA() {
        return a;
    }

    public void setA(A a) {
        this.a = a;
    }
}

Whenever two objects are associated, such as in the case with your @OneToOne , changes to either side of the relationship will not cascade an audit of the other side of that relationship unless you: 每当关联两个对象时(例如@OneToOne ,对关系任何一方的更改都不会级联对该关系另一端的审核,除非您:

  1. Modify the relationship such as pointing B to a different instance of A 修改关系,例如将B指向不同的A实例
  2. Modify an attribute on the other side of the relationship 修改关系另一侧的属性

From a simple Change Data Capture (CDC) perspective, these rules make sense. 从简单的变更数据捕获(CDC)角度来看,这些规则是有意义的。 What I mean is the attributes on the associated entity that wasn't modified in your screen in any meaningful way does not justify an audit row, so we only capture the entity instance on the side which actually had any changes detected. 我的意思是在屏幕上未以任何有意义的方式修改的关联实体上的属性不能证明审计行的合理性,因此我们只捕获实际检测到任何更改的一侧的实体实例。

In a complex domain model, we have multitudes of relationships between objects. 在复杂的域模型中,我们在对象之间存在大量关系。 Just imagine the sheer number of audit rows generated if we propagated those change events across relationship boundaries. 想象一下,如果我们跨越关系边界传播这些更改事件,则会生成大量的审计行。 You could easily have a situation where a change of a basic string attribute would lead to the auditing of your entire object graph of entities related to the changed entity. 您可能很容易遇到更改基本字符串属性会导致审核与更改实体相关的实体的整个对象图的情况。

You have a couple ways you can accomplish what you want: 你有几种方法可以完成你想要的任务:

  1. Use conditional auditing 使用条件审计
  2. Use an attribute to force auditing to occur. 使用属性强制进行审核。

There is an entire section in the documentation on Conditional Auditing . 有条件审计的文档中有一整节。 Its original purpose was to control whether certain changes on audit entities should allow an entity to be recorded, but you could also use it to navigate very specific entity associated and force work unit audit operations on related entities. 其最初目的是控制审计实体的某些变更是否应允许记录实体,但您也可以使用它来导航非常具体的实体,并强制对相关实体进行工作单位审计操作。 This is an expert approach and not one I recommend for users who are not familiar and how it operates. 这是一种专家方法,而不是我建议不熟悉的用户及其运作方式。 In 6.0, I'm looking to simplify this in some ways to make this less intrusive than what users must currently do. 在6.0中,我希望在某些方面简化这一过程,以减少用户当前必须执行的操作。

The simplest of ways is to do (2). 最简单的方法是做(2)。 For this to work, you would add a new field on both entities that would store something such as a timestamp value. 为此,您可以在两个实体上添加一个新字段,用于存储诸如时间戳值之类的内容。 Whenever a change happens to either entity in your UI, you would set the timestamp for both entities which effectively will force Envers to audit both entities in the same transaction, accomplishing precisely what you want. 每当UI中的任一实体发生更改时,您都会为两个实体设置时间戳,这将有效地强制Envers审核同一事务中的两个实体,从而精确地完成您想要的任务。

This is something that users bring up frequently, so I've created HHH-13362 where we can have a more detailed discussion on how to best develop and improve on this in a less intrusive way. 这是用户经常提出的,所以我创建了HHH-13362 ,我们可以在这里更详细地讨论如何以较少侵入的方式最好地开发和改进它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM