3

Have two entities Instrument and Definition.

When instrumentCode changed Envers create audited record only for Instrument.

I want that when instrumentCode changed Envers create audited records for both Instrument and Definition entities. How it is possible to do, and is it possible?

I've played with @AuditedJoinTable, @AuditedMappedBy but without luck.

@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();
}
Dragan Bozanovic
  • 23,102
  • 5
  • 43
  • 110
nahab
  • 1,308
  • 17
  • 38

1 Answers1

1

I am not aware of any out of the box available configurations for that already available at this moment (although it seems that something similar is being developed).

Probably you could merge the revisions of the two together manually via audit queries when fetching history to keep the audit records in the database fully normalized.

Or you could introduce a dummy column in the parent entity and update it whenever children change, as shown in 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();
        }
    }
}
Dragan Bozanovic
  • 23,102
  • 5
  • 43
  • 110
  • yeah, also thought about dummy field or.. rewrite Envers listeners completly - but this will not be Envers than ). Thanks for the overall answer – nahab Jun 13 '19 at 12:52