I am using hibernate envers to audit my entity. I have entity with next fields:
public class Settings
@Id
@Column(length = 80)
private String key;
@NotNull
@Column(length = 1200)
private String value;
@Version
@Column(columnDefinition = "numeric")
private Integer version;
}
It contains key-value pairs. Some of fields in this table are updated automatically. The question is: is it possible to insert or not insert record into _AUDIT table depending on value of 'key' property? Example: There are records in my table:
|KEY |VALUE |VERSION
_________________________________
|laskCheckDate|12-01-2017|0
|numberOfsmth |3 |0
I want to insert record to _AUDIT table if numberOfsmth is updated/deleted, but NOT insert if laskCheckDate is updated.
What you would need to do is extend the EnversPostUpdateEventListenerImpl
event listener class and add your logic to check for the necessary entity type and values and decide whether to call into the super-class to audit the update or not.
Unfortunately the above approach is a bit intrusive for the novice user and I would certainly not recommend doing this if you're not super familar with Hibernate ORM and Envers.
There are some thoughts on conditional auditing in HHH-11326 which is tentatively planned for Envers 6.0 where you can influence auditing based on hooks you tie into your entities through annotations.
Should you decide to move forward and extend the listeners in 5.x, just be mindful that you should always allow the INSERT
of your entity to occur. This becomes extremely important if you're using the ValidityAuditStrategy
as the UPDATE
expects an INSERT
revision type to exist in the table or else the strategy asserts.
If all you want to control are UPDATE
s, then this should not be a problem for you regardless of which strategy you leverage.