title: Hibernate PostUpdateEventListener is invoking for every Jpa call
body: <p>I am using Spring Data JPA with hibernate 5.3.7 core.I have written a custom listener that implements PostUpdateEventListener.My requirement is to invoke a callback method on each Database update i.e. only for update operation call of the entity.</p>
  
  <p>But <code>onPostUpdate</code> of <code>PostUpdateEventListener</code> is invoking on every Jpa call i.e.  irrespective of DB operation. This is strange behavior for me. Or the listener behaves in same way?</p>
  
  <p>I am using Hibernate 5.3.7 and not able to see much documentation in detail other than the following <a href="https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/event/spi/PostUpdateEventListener.html#requiresPostCommitHandling-org.hibernate.persister.entity.EntityPersister-" rel="nofollow noreferrer">link</a></p>
  
  <p>Also attaching the classes which is used to register my custom listener and required configuration.</p>
  
  <ol>
  <li><p>CustomEventListener class : </p>
  
  <pre><code>import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  
  import org.hibernate.event.spi.PostUpdateEvent;
  import org.hibernate.event.spi.PostUpdateEventListener;
  import org.hibernate.persister.entity.EntityPersister;
  import org.springframework.stereotype.Component;
  
  @Component
  public class CustomEventListener implements PostUpdateEventListener {
  
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
  
      public static final CustomEventListener INSTANCE = new CustomEventListener();
      @PersistenceContext
      private EntityManager entityManager;
      /* (non-Javadoc)
       * @see org.hibernate.event.spi.PostCollectionUpdateEventListener#onPostUpdateCollection(org.hibernate.event.spi.PostCollectionUpdateEvent)
       */
  
      @Override
      public boolean requiresPostCommitHanding(EntityPersister persister) {
          // TODO Auto-generated method stub
  
          return false;
      }
  
      /* (non-Javadoc)
       * @see org.hibernate.event.spi.PostUpdateEventListener#onPostUpdate(org.hibernate.event.spi.PostUpdateEvent)
       */
      @Override
      public void onPostUpdate(PostUpdateEvent event) {
          // TODO Auto-generated method stub
          System.out.println("hello");
      }
  
  }`
  </code></pre></li>
  <li><p>HibernateConfig class : used to configure <code>hibernate.integrator_provider</code> property in the existing configuration</p>
  
  <pre><code>import java.util.Collections;
  import java.util.Map;
  
  import org.hibernate.jpa.boot.spi.IntegratorProvider;
  import org.springframework.boot.autoconfigure.orm.jpa.HibernatePropertiesCustomizer;
  import org.springframework.stereotype.Component;
  
  /**
   * @author satyam.kaushik
   * @version  Last modified 09-Apr-2019
   */
  
  @Component
  public class HibernateConfig implements HibernatePropertiesCustomizer {
  
      @Override
      public void customize(Map&lt;String, Object&gt; hibernateProperties) {
          hibernateProperties.put("hibernate.integrator_provider",
                  (IntegratorProvider) () -&gt; Collections.singletonList(MetadataExtractorIntegrator.INSTANCE));
      }
  }
  </code></pre></li>
  <li><p>MetadataExtractorIntegrator class to register listener
      `</p>
  
  <pre><code>import org.hibernate.boot.Metadata;
  import org.hibernate.engine.spi.SessionFactoryImplementor;
  import org.hibernate.event.service.spi.EventListenerRegistry;
  import org.hibernate.event.spi.EventType;
  import org.hibernate.integrator.spi.Integrator;
  import org.hibernate.service.spi.SessionFactoryServiceRegistry;
  
  public class MetadataExtractorIntegrator implements Integrator {
  
      public static final MetadataExtractorIntegrator INSTANCE = new MetadataExtractorIntegrator();
  
      @Override
      public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory,
              SessionFactoryServiceRegistry serviceRegistry) {
  
          final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);
  
          eventListenerRegistry.appendListeners(EventType.POST_UPDATE, CustomEventListener.INSTANCE);
  
      }
  
      @Override
      public void disintegrate(SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
  
      }
  }`
  </code></pre>
  
  <p>I also tried other in built listeners provided by API i.e. <code>SaveOrUpdateEventListener</code> or <code>MergeEventListener</code> but nothing is working for me. It looks like the listener is invoking for each JPA call instead of only update call.</p></li>
  </ol>
  
  <p>Also attaching the below logs for the Get request which is causing issue (i.e. invoking listener method which should not suppose to call)</p>
  
  <p>0:0:0:0:0:0:0:1 - - [09/Apr/2019:16:38:38 +0530] "OPTIONS /rippsadm/rest/device?sort-column=&amp;sort-order=asc&amp;page-no=1&amp;page-size=15 HTTP/1.1" 200 -</p>
  
  <p>0:0:0:0:0:0:0:1 - - [09/Apr/2019:16:38:38 +0530] "OPTIONS /rippsadm/rest/device/filter HTTP/1.1" 200 -</p>
  
  <p>0:0:0:0:0:0:0:1 - - [09/Apr/2019:16:38:41 +0530] "GET /rippsadm/rest/device/filter HTTP/1.1" 200 1918</p>
  
  <p>0:0:0:0:0:0:0:1 - - [09/Apr/2019:16:38:41 +0530] "GET /rippsadm/rest/device?sort-column=&amp;sort-order=asc&amp;page-no=1&amp;page-size=15 HTTP/1.1" 200 6422</p>