我只想审核更新和删除操作,但是休眠envers也会记录INSERT操作,如何停止审核插入操作?
application.properties
spring.jpa.properties.org.hibernate.envers.default_catalog=demo_audit
spring.jpa.properties.org.hibernate.envers.store_data_at_delete=true
spring.jpa.properties.hibernate.listeners.envers.autoRegister=false
spring.jpa.properties.hibernate.envers.autoRegisterListeners=false
您需要通过事件来中断envers =>有关条件审核的详细说明,请参见here。这些步骤适用于spring-boot:
package com.example.stackoverflow1.audit;
import com.example.stackoverflow1.model.Soup;
import org.hibernate.envers.boot.internal.EnversService;
import org.hibernate.envers.event.spi.EnversPostInsertEventListenerImpl;
import org.hibernate.event.spi.PostInsertEvent;
public class CustomAuditEventListenerPostInsert extends EnversPostInsertEventListenerImpl {
public CustomAuditEventListenerPostInsert(EnversService enversService) {
super(enversService);
}
@Override
public void onPostInsert(PostInsertEvent event) {
// super.onPostInsert(event); if this is called, the audit is executed.
// without it there is no audit entry
}
}
import lombok.AllArgsConstructor;
import org.hibernate.envers.boot.internal.EnversService;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
import org.hibernate.jpa.HibernateEntityManagerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
@AllArgsConstructor
public class AuditConfig {
private HibernateEntityManagerFactory hibernateEntityManagerFactory;
@PostConstruct
public void registerEnversListeners() {
EnversService enversService =
hibernateEntityManagerFactory
.getSessionFactory()
.getServiceRegistry()
.getService(EnversService.class);
EventListenerRegistry listenerRegistry = hibernateEntityManagerFactory.getSessionFactory().getServiceRegistry().getService(EventListenerRegistry.class);
listenerRegistry.setListeners(EventType.POST_INSERT, new CustomAuditEventListenerPostInsert(enversService));
}
}
Edit1:仅当父实体没有审核的子实体时,此方法才有效。否则,将在创建后添加更新的语句。因此,我还需要stackoverflow
的答案Edit2:签出此手动触发器以获取审核条目-https://developer.jboss.org/thread/274280