Hibernate Envers 也审核 oneToMany 方面,即使它没有任何变化

Hibernate Envers audit oneToMany side too, even if it has no changes

有两个实体 InstrumentDefinition

instrumentCode 更改时,Envers 仅为 Instrument 创建审核记录。

我希望当 instrumentCode 更改时,Envers 会为 InstrumentDefinition 实体创建审计记录。怎么可能,有可能吗?

我玩过@AuditedJoinTable、@AuditedMappedBy 但运气不好。

@Audited
@Getter
@Entity
public class Instrument {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "instrument_code")
    protected String instrumentCode;

    @ManyToOne(optional = false, fetch = FetchType.LAZY, targetEntity = Definition.class)
    @JoinColumn(name = "definition_id", nullable = false)
    private Definition definition;
}
//-----
@Audited
@Getter
@Entity
public class Definition {

    @Id
    @Column(nullable = false)
    protected String id;

    @OneToMany(mappedBy = "definition",
            orphanRemoval = true,
            cascade = CascadeType.ALL,
            targetEntity = Instrument.class)
    private Set<Instrument> instruments = Sets.newHashSet();
}

我不知道目前已经可用的任何开箱即用的可用配置(尽管看起来类似 is being developed)。

也许您可以在获取历史记录时通过 audit queries 手动将两者的修订合并在一起,以保持数据库中的审计记录完全规范化。

或者您可以在父实体中引入一个虚拟列,并在子实体发生变化时更新它,如 this answer 所示:

@Entity
public class A {
    private Date lastModified;

    @OneToMany(mappedBy = "a", cascade = CascadeType.ALL)
    private List<B> blist;

    public void touch() {
        lastModified = new Date();
    }
}

public class B {
    @ManyToOne
    private A a; 

    @PreUpdate
    public void ensureParentUpdated() {
        if (a != null) {
            a.touch();
        }
    }
}