简体   繁体   English

Hibernate - 实体审计

[英]Hibernate - Entity audit

I have one entity and I want to track all changes therefore I created new Entity for audit. 我有一个实体,我想跟踪所有更改,因此我创建了新的实体进行审计。 Below is my primary entity: 以下是我的主要实体:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "primary")
public class PrimaryEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "primary_id")
    private Long id;

    private String name;

    @LazyCollection(LazyCollectionOption.FALSE)
    @ElementCollection
    @CollectionTable(
            name = "primary_attachments",
            joinColumns = @JoinColumn(name = "primary_id")
    )
    private List<String> attachments;

    @CreatedDate
    @Temporal(TemporalType.DATE)
    private Date createDate;

    @LastModifiedDate
    @Temporal(TemporalType.DATE)
    private Date lastModifiedDate;
}

And below is my entity for audit: 以下是我的审计实体:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "primary_audit")
public class PrimaryEntityAudit {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "audit_id")
    private Long id;

    @NotNull
    @Column(name = "primary_entity_id")
    private Long primaryId;

    private String name;

    @LazyCollection(LazyCollectionOption.FALSE)
    @ElementCollection
    @CollectionTable(
            name = "primary_attachments_audit",
            joinColumns = @JoinColumn(name = "primary_entity_id")
    )
    private List<String> attachments = new ArrayList<>();

    @CreatedDate
    @Temporal(TemporalType.DATE)
    private Date createDate;

    public PrimaryEntityAudit(PrimaryEntity primaryEntity) {
        this.primaryId = primaryEntity.getId();
        this.attachments.addAll(primaryEntity.getAttachments());
        this.createDate = new Date();
    }
}

And before update primary entity I create new PrimaryEntityAudit and save this object and then update the primary entity. 在更新主要实体之前,我创建新的PrimaryEntityAudit并保存此对象,然后更新主要实体。 And operation is successful and object PrimaryEntityAudit is saved, but attachments from PrimaryEntityAudit aren't saved. 并且操作成功并保存对象PrimaryEntityAudit,但不保存来自PrimaryEntityAudit的附件。

I tried also in constructor of ProjectEntityAudit do setAttachments, but then I got an Exception: HibernateExcpetion: Found shared references to collection. 我也尝试在ProjectEntityAudit的构造函数中执行setAttachments,但后来我得到了一个例外:HibernateExcpetion:找到了对集合的共享引用。

How should I map the collections of audit for saving old state of PrimaryEntity attachments? 我应该如何映射审计集合以保存旧的PrimaryEntity附件状态?

You should look at the following hibernate module Envers 您应该查看以下hibernate模块Envers

It provides features for versioning and auditing 它提供版本控制和审计功能

It is preferable to not reinvent the wheel except if you have technicals constraints which prevent you to use some frameworks or others. 除非你有技术限制阻止你使用某些框架或其他框架,否则最好不要重新发明轮子。

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

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