问题
I am using Hibernate 4.0 with envers
When I do an update or insert, data is getting saved into audit table with mode a 0 and 1
I would like to disable audit on post-insert. can someone please, let me know how to disable it.
I have read that hibernate from version 4.0, it automatically registers the events for auditing.
I there a way we can disable audit on insert?
回答1:
hibernate.envers.autoRegisterListeners - controls whether the standard Envers listeners are auto-registered.
You can register custom listeners to allow or prevent custom event to works.
Here is steps from hibernate guide:
- Turn off automatic Envers event listeners registration by setting thehibernate.listeners.envers.autoRegister Hibernate property to false.
- Create subclasses for appropriate event listeners. For example, if you want to conditionally audit entity insertions, extend the org.hibernate.envers.eventEnversPostInsertEventListenerImpl class. Place the conditional-auditing logic in the subclasses, call the super method if auditing should be performed.
- Create your own implementation of org.hibernate.integrator.spi.Integrator, similar to org.hibernate.envers.event.EnversIntegrator. Use your event listener classes instead of the default ones.
- For the integrator to be automatically used when Hibernate starts up, you will need to add aMETA-INF/services/org.hibernate.integrator.spi.Integrator file to your jar. The file should contain the fully qualified name of the class implementing the interface.
for more detail go to link Code sample
import org.hibernate.envers.configuration.spi.AuditConfiguration;
import org.hibernate.envers.event.spi.EnversPostInsertEventListenerImpl;
import org.hibernate.event.spi.PostInsertEvent;
public class SprEnversPostInsertEventListenerImpl extends EnversPostInsertEventListenerImpl {
private static final long serialVersionUID = 1L;
public SprEnversPostInsertEventListenerImpl(AuditConfiguration enversConfiguration) {
super(enversConfiguration);
}
@Override
public void onPostInsert(PostInsertEvent event) {
super.onPostInsert(event);
}
}
custom integrator's Code sample
public class EnverseCustomIntegrator extends EnversIntegrator {
public static final String AUTO_REGISTER = "hibernate.listeners.envers.autoRegister";
private AuditConfiguration enversConfiguration;
@Override
public void integrate(org.hibernate.cfg.Configuration configuration, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
final EventListenerRegistry listenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);
listenerRegistry.addDuplicationStrategy(EnversListenerDuplicationStrategy.INSTANCE);
enversConfiguration = AuditConfiguration.getFor(configuration, serviceRegistry.getService(ClassLoaderService.class));
if (enversConfiguration.getEntCfg().hasAuditedEntities()) {
listenerRegistry.appendListeners(EventType.POST_DELETE, new SprEnversPostDeleteEventListenerImpl(enversConfiguration));
listenerRegistry.appendListeners(EventType.POST_INSERT, new SprEnversPostInsertEventListenerImpl(enversConfiguration));
listenerRegistry.appendListeners(EventType.POST_UPDATE, new SprEnversPostUpdateEventListenerImpl(enversConfiguration));
listenerRegistry.appendListeners(EventType.POST_COLLECTION_RECREATE, new SprEnversPostCollectionRecreateEventListenerImpl(enversConfiguration));
listenerRegistry.appendListeners(EventType.PRE_COLLECTION_REMOVE, new SprEnversPreCollectionRemoveEventListenerImpl(enversConfiguration));
listenerRegistry.appendListeners(EventType.PRE_COLLECTION_UPDATE, new SprEnversPreCollectionUpdateEventListenerImpl(enversConfiguration));
}
}
}
Persistance Context configuration
<property name="hibernate.integration.envers.enabled" value="true"/>
<property name="hibernate.envers.autoRegisterListeners" value="true"/>
src/main/resources/META-INF/services/org.hibernate.integrator.spi.Integrator content sample is:
mycustom.audit.package.audit.EnverseCustomIntegrator
回答2:
remove
@Audited
annotations on entities,if they are present on entity class/field names.If you have custom tables to store audit log,remove those entities
来源:https://stackoverflow.com/questions/43456454/how-to-prevent-audit-on-insert-using-envers-4-hibernate