【问题标题】:Hibernate Envers audit oneToMany side too, even if it has no changesHibernate Envers 也审计 oneToMany 端,即使它没有任何变化
【发布时间】:2019-06-12 21:22:32
【问题描述】:

有两个实体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();
}

【问题讨论】:

    标签: java hibernate hibernate-envers


    【解决方案1】:

    我不知道目前已经有任何开箱即用的可用配置(尽管看起来类似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();
            }
        }
    }
    

    【讨论】:

    • 是的,还考虑了虚拟字段或.. 完全重写 Envers 侦听器 - 但这不会是 Envers 比)。感谢您的整体回答
    猜你喜欢
    • 2018-03-10
    • 2023-03-24
    • 2013-11-16
    • 2012-12-05
    • 2016-11-15
    • 1970-01-01
    • 2015-12-14
    • 2018-04-18
    • 2013-05-06
    相关资源
    最近更新 更多