如何停止审核休眠中的创建操作?

问题描述 投票:1回答:1

我只想审核更新和删除操作,但是休眠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
hibernate spring-boot hibernate-envers
1个回答
0
投票

您需要通过事件来中断envers =>有关条件审核的详细说明,请参见here。这些步骤适用于spring-boot:

  1. 重写onPostInsert方法
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
    }

}

  1. 您需要注册自定义onPostInsertListener才能进行转换:
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

© www.soinside.com 2019 - 2024. All rights reserved.