Envers audits parent entity when inserting a sub
I have a Store/Clerks classes in my application, that are related via the "storeId" foreign key in the "clerks" DB table , and with the Hibernate annotations given in the following code:
Store.java :
@Entity
@Audited
@Table(name="stores")
Public Class Store {
private Set<Clerks> clerks;
//....
@OneToMany(fetch = FetchType.LAZY, mappedBy = "store")
public Set<Clerks> getClerks() {
return clerks;
}
}
Clerk.java:
@Entity
@Audited
@Table(name="clerks")
Public Class Clerk {
private Store store;
//....
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "storeId",referencedColumnName = "storeId")
public Store getStore() {
return store;
}
}
When I'm inserting (persisting) the new Clerk, Envers makes entries in audit tables of both entities ("stores_aud" and "clerks_aud") . But, when I'm updating the existing Clerk, it only makes an entry in the "clerks_aud" table.
Can anyone explain to me, why this is happening, and how to enforce Envers to behave the same in both of the cases (insert and update)?
Thank you
When you add a new Clerk
to Store
, Store#clerks
collection is altered, which results in new audit entry for Store
. When Clerk
is changed, no fields of Store
are changed, so no audit entry is generated for it, just for Clerk
.
If you want to also generate audit entry for Store
when Clerk
is updated, you will have to handle it yourself. One common solution for this is to have something like lastUpdated
column on Store
, which you would update whenever something has changed.
上一篇: 启动时填写审计表
下一篇: Envers在插入子时会审核父实体