Conditional Envers Auditing

后端 未结 5 1417
死守一世寂寞
死守一世寂寞 2020-12-31 13:58

I have a requirement where I want to audit records only on change of Status field. I\'ve followed documentation chapter tutorial \"15.8. Conditional auditing\".

Step

相关标签:
5条回答
  • 2020-12-31 14:41

    Try to place integrator file into:

    sample.war\WEB-INF\classes\META-INF\services\...
    
    0 讨论(0)
  • 2020-12-31 14:48

    Maybe...

    In my case, I use Maven, and I had to include in the pom.xml, the following line : <include>**/*.Integrator</include>, because the file was not packaged in the .ear.

    My pom.xml:

    <resources>
           <resource>
               <directory>src/main/resources</directory>
               <filtering>true</filtering>
               <includes>
                    <include>**/*.xml</include>
                   <include>**/*.Integrator</include>
               </includes>
           </resource>
           ...
    
    0 讨论(0)
  • 2020-12-31 14:49

    creating a file org.hibernate.integrator.spi.Integrator (containing the qualified name of my custom integrator) in a folder META-INF/services/ under src/main/resources of my maven project made my custom integrator code be called.

    0 讨论(0)
  • 2020-12-31 14:56

    Here is a Spring-only solution for the conditional Envers auditing without an ugly META-INF folder etc. All what you need is a bean in your configuration class and a 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;
      }
    

    and

      public class CustomEnversEventListener extends EnversPostUpdateEventListenerImpl {
    
        CustomEnversEventListener(EnversService enversService) {
          super(enversService);
        }
    
        @Override
        public void onPostUpdate(PostUpdateEvent event) {
    
          // custom conditional stuff
    
          super.onPostUpdate(event);
        }
      }
    
    

    If you want to customise only one listener i.e. EnversPostUpdateEventListener, you don't need to disable hibernate.listeners.envers.autoRegister in order to let Envers register the other listener.

    Then you can override Envers listeners by listenerRegistry.setListeners or append by listenerRegistry.appendListeners

    0 讨论(0)
  • 2020-12-31 15:00

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

    Adding lazyInit = true, triggered the custom integrator for me.

    0 讨论(0)
提交回复
热议问题