If you want to turn off auditing in runtime based on some state, you can make your own custom subclass of AuditEventListener
and pass an instance of that type into IntegrateWithEnvers
method.
In your subclass, you can override OnPostDelete
, OnPostInsert
, OnPostRecreateCollection
, OnPostUpdate
, OnPreRemoveCollection
and OnPreUpdateCollection
. In your case, you should probably check evt.Entity
and evt.AffectedOwnerOrNull
. If you want to audit, simply call base method, if you don't want to audit do nothing in your implementation.
Note however that you should probably just doing this if you just use Envers for simple logging. If you use it to recreate historical instances, "removing" auditing for some historical events may cause problems when loading historical instances. If that's the case for you it's only safe to do this if an entity either has or hasn't got Orders
for all its lifetime.