title: Errror with method postFlush in Hibernate Interceptors
body: <p>I want to log in my database the operations executed over a table (inserts, updates and deletes). I've been reading about Interceptors, so I implemented my own. </p>
  
  <p>For example, when an Insertion is performed I do this:</p>
  
  <pre><code>@Override
  public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
      if (entity instanceof Person) {
          System.out.println("Se ha insertado una persona");
          operation = "Insert";
          date = Calendar.getInstance().getTime();
          return true;
      }
      return false;
  }
  </code></pre>
  
  <p>Finally in the postFlush I do this: </p>
  
  <pre><code>// called after committed into database
  public void postFlush() {
      System.out.println("postFlush");
      Session ses = HibernateUtil.getSessionFactory().openSession();
      ses.getTransaction().begin();
      Registry reg = new Registry(operation, date);
      ses.save(reg);
      ses.getTransaction().commit();
  }
  </code></pre>
  
  <p>In Registry is where I store the information about the operations executed on the table I'm checking. </p>
  
  <p>I had a lot of problems while calling the interceptor on the class with the methods to save, update and delete. After hours reading on the Internet and here (I love this site jeje) I was able to call the interceptor this way:</p>
  
  <pre><code>    Session session = this.sessionFactory.withOptions().interceptor(new MyInterceptor).openSession();
  </code></pre>
  
  <p>I run my simple web app to save the name and the country of a person. I was able to store the person's information, but my Registry table was empty. I debugged my code and the method postFlush was never been called. Any ideas? I have followed various tutorials and some recommendations, but nothing seems to work.</p>
  
  <p>The last thing I tried was in a very "dirty" way. I changed my code from this: </p>
  
  <pre><code>@Override
  @Transactional
  public void addPerson(Person p) {
      Session session = this.sessionFactory.withOptions().interceptor(new MyInterceptor).openSession();
      session.save(p);
      logger.info("Person saved successfully, Person Details="+p);
  }
  </code></pre>
  
  <p>to this (horrendous) way: </p>
  
  <pre><code>@Override
  @Transactional
  public void addPerson(Person p) {
      MyInterceptor m = new MyInterceptor();
      Session session = this.sessionFactory.withOptions().interceptor(m).openSession();
      Transaction tx = session.beginTransaction();
      session.save(p);
      tx.commit();
      session.flush();
      m.postFlush();
      logger.info("Person saved successfully, Person Details="+p);
  }
  </code></pre>
  
  <p>And it worked! I was able to store information of persons and also in Registry, but this isn't the right way eh?</p>
  
  <p>Thanks in advance</p>