9

我有一个要求,我只想在状态字段更改时审核记录。我遵循了文档章节教程“15.8。条件审计”。

第 1 步:关闭自动 Envers 事件侦听器注册。我有以下内容:

<prop key="hibernate.listeners.envers.autoRegister">false</prop>

第 2 步:为适当的事件侦听器创建子类。

public class DeleteEnversListener extends EnversPostDeleteEventListenerImpl {   
    private static final long serialVersionUID = 5906427978349712224L;
    private static Log log = LogFactory.getLog(DeleteEnversListener.class);

    public DeleteEnversListener(AuditConfiguration enversConfiguration) {
        super(enversConfiguration);
    }

    @Override
    public void onPostDelete(PostDeleteEvent event) {
        log.info("!!! just logging entity !! "+ event.getEntity());
        super.onPostDelete(event);
    }   
}

以类似的方式,我有

  • 插入环境监听器
  • 更新环境监听器
  • 删除EnversListener
  • CollectionRecreateEnversListener
  • PreCollectionRemoveEnversListener
  • PreCollectionUpdateEnversListener

第 3 步:创建自己的实现org.hibernate.integrator.spi.Integrator

public class CustomEnversIntegrator extends EnversIntegrator   {

    private static Log log = LogFactory.getLog(CustomEnversIntegrator.class);

    @Override
    public void integrate(Configuration configuration,
            SessionFactoryImplementor sessionFactory,
            SessionFactoryServiceRegistry serviceRegistry) {

        super.integrate(configuration, sessionFactory, serviceRegistry);
        final AuditConfiguration enversConfiguration = AuditConfiguration.getFor( configuration, serviceRegistry.getService( ClassLoaderService.class ) );
        EventListenerRegistry listenerRegistry = serviceRegistry.getService( EventListenerRegistry.class );

        System.out.println("Registering event listeners");
        if (enversConfiguration.getEntCfg().hasAuditedEntities()) {
            listenerRegistry.appendListeners(EventType.POST_INSERT, new InsertEnversListener(enversConfiguration));
            listenerRegistry.appendListeners(EventType.POST_UPDATE, new UpdateEnversListener(enversConfiguration));
            listenerRegistry.appendListeners(EventType.POST_DELETE, new DeleteEnversListener(enversConfiguration ) );
            listenerRegistry.appendListeners(EventType.POST_COLLECTION_RECREATE, new CollectionRecreateEnversListener(enversConfiguration ) );
            listenerRegistry.appendListeners(EventType.PRE_COLLECTION_REMOVE, new PreCollectionRemoveEnversListener(enversConfiguration ) );
            listenerRegistry.appendListeners(EventType.PRE_COLLECTION_UPDATE, new PreCollectionUpdateEnversListener(enversConfiguration ) );
        }

    }
}

第 4 步:要在 Hibernate 启动时自动使用积分器,您需要添加一个META-INF/services/org.hibernate.integrator.spi.Integrator文件。这是org.hibernate.integrator.spi.Integrator文件的内容

com.hib.sample.listener.CustomEnversIntegrator

我不确定,如果我错过了什么。我正在使用 JBOSS AS 7.0 和 Hibernate 4.1.8

4

5 回答 5

11

这是一个仅用于条件Envers审计的 Spring 解决方案,没有丑陋的META-INF文件夹等。您所需要的只是配置类中的一个 bean 和一个CustomEnversEventListener.

@Bean
public EventListenerRegistry listenerRegistry(EntityManagerFactory entityManagerFactory) {
    ServiceRegistryImplementor serviceRegistry = entityManagerFactory.unwrap(SessionFactoryImpl.class).getServiceRegistry();

    final EnversService enversService = serviceRegistry.getService(EnversService.class);
    EventListenerRegistry listenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);

    listenerRegistry.setListeners(EventType.POST_UPDATE, new CustomEnversEventListener(enversService));
    return listenerRegistry;
  }

  public class CustomEnversEventListener extends EnversPostUpdateEventListenerImpl {

    CustomEnversEventListener(EnversService enversService) {
      super(enversService);
    }

    @Override
    public void onPostUpdate(PostUpdateEvent event) {

      // custom conditional stuff

      super.onPostUpdate(event);
    }
  }

如果您只想自定义一个监听器,即 EnversPostUpdateEventListener,则无需禁用hibernate.listeners.envers.autoRegister即可让 Envers 注册另一个监听器。

然后你可以覆盖Envers监听器listenerRegistry.setListeners或附加listenerRegistry.appendListeners

于 2019-03-28T15:51:47.073 回答
1

尝试将积分器文件放入:

sample.war\WEB-INF\classes\META-INF\services\...
于 2014-02-04T13:59:35.630 回答
0

也许...

在我的情况下,我使用 Maven,并且我必须包含在pom.xml以下行 : <include>**/*.Integrator</include>中,因为该文件未打包在.ear.

我的pom.xml

<resources>
       <resource>
           <directory>src/main/resources</directory>
           <filtering>true</filtering>
           <includes>
                <include>**/*.xml</include>
               <include>**/*.Integrator</include>
           </includes>
       </resource>
       ...
于 2015-05-11T20:38:09.707 回答
0

@ComponentScan(basePackages = {"com.example.demo"},lazyInit = true)

添加lazyInit = true,为我触发了自定义积分器。

于 2018-10-25T09:58:43.427 回答
0

在我的 maven 项目的src/main/resources下的META-INF/services/文件夹中创建文件org.hibernate.integrator.spi.Integrator(包含我的自定义集成器的限定名称)使我的自定义集成器代码被调用。

于 2016-05-06T13:34:01.040 回答