title: Hibernate Interceptors - Why is onFlushDirty called after onSave?
body: <h1>The Plan</h1>
  
  <p>I am using Hibernate to implement createDate and lastUpdate Timestamps for a little project. I use an EmptyInterceptor and overload the provided methods based on the proposed solution i found <a href="https://stackoverflow.com/a/7844107/3289468">here</a>.
  The solution works fine unless one little detail. I want to add a column indicating if an object has been updated already. I know I could achieve this by simply comparing if there is a difference in the two created and updated timestamps, but I need to have this field indicating that there was an update. </p>
  
  <p>I use the onSave method which gets called when a new object is stored to set the wasUpdated value to 'N', indicating that there was no update. In the onFlushDirty() method I set this value to 'Y'.</p>
  
  <h1>The Problem</h1>
  
  <p>I would expext that when I create and persist a new Object, that the createDate and the lastUpdate fields have the same Date, but the wasUpdated field is set to 'N' as there was no update. I only use session.save() in my code, there is no session.update() and also no session.saveOrUpdate(). The logs of Hibernate indicate that there is actually an Update, which sets the wasUpdated value to 'Y'. </p>
  
  <p><strong>What can be the source of this update? Where is it triggered?</strong> </p>
  
  <h1>Object initialization and persistence</h1>
  
  <p>I disabled auto-commit in the hibernate.cfg.xml.</p>
  
  <pre><code>&lt;property name="hibernate.connection.autocommit"&gt;false&lt;/property&gt;
  </code></pre>
  
  <p>This is how I create the object:</p>
  
  <pre><code>ExampleObject ex = new ExampleObject();
  ex.setValue("TestStringValue");
  this.session = HibernateUtil.getSessionFactory().openSession();
  this.session.beginTransaction();
  this.session.save(ex);
  this.session.getTransaction().commit();
  this.session.close();
  </code></pre>
  
  <h1>The Interceptor</h1>
  
  <pre><code>@Override
      public boolean onSave(Object entity, Serializable id, Object[] state,
                    String[] propertyNames, Type[] types) {
  
  
      if (entity instanceof TimeStamped) {
  
          Date insertDate = new Date();
          int indexOfCreateDateColumn = ArrayUtils.indexOf(propertyNames, "createdDate");
          int indexOfUpdatedDateColumn = ArrayUtils.indexOf(propertyNames, "lastUpdatedDate");
          int indexOfWasUpdated = ArrayUtils.indexOf(propertyNames, "wasUpdated");
  
          state[indexOfCreateDateColumn] =insertDate;
          state[indexOfUpdatedDateColumn] =insertDate;
          state[indexOfWasUpdated] ='N';
  
          return true;
      }
      return false;
      }
  </code></pre>
  
  <p>The second method is for setting the lastUpdatedDate and setting the wasUpdated field to 'Y'.</p>
  
  <pre><code>@Override
  public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState,
                      Object[] previousState, String[] propertyNames, Type[] types) {
  
      if (entity instanceof TimeStamped) {
  
          int indexOfLastUpdate = ArrayUtils.indexOf(propertyNames, "lastUpdatedDate");
          int indexOfWasUpdated = ArrayUtils.indexOf(propertyNames, "wasUpdated");
  
          currentState[indexOfLastUpdate] = new Date();
          currentState[indexOfWasUpdated] ='Y';
  
  
          return true;
      }
      return false;
  }
  </code></pre>
  
  <h1>HibernateUtil</h1>
  
  <p>I use this configuration for the session.</p>
  
  <pre><code>public class HibernateUtil {
      private static SessionFactory sessionFactory;
      private static ServiceRegistry serviceRegistry;
  
      static {
      try {
  
          Configuration configuration = new Configuration().setInterceptor(new TimeStampInterceptor());
          configuration.configure();
  
          serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
          sessionFactory = configuration.buildSessionFactory(serviceRegistry);
  
  
      } catch (HibernateException he) {
          System.err.println("Error creating Session: " + he);
          throw new ExceptionInInitializerError(he);
      }
      }
  
      public static SessionFactory getSessionFactory() {
      return sessionFactory;
      }
  }
  </code></pre>
  
  <h1>Versions</h1>
  
  <p>I use Maven and Java 1.7.0_40.</p>
  
  <pre><code>    &lt;!--. Hibernate --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
          &lt;version&gt;4.3.4.Final&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>