title: PreInsert and PreUpdate Event Listener in hibernate
body: <p>I have used <code>PreInsertEventListener</code> and <code>PreUpdateEventListener</code> Event Listener to insert created date and updated date in table.<br>
  The problem i am facing is that when i save entity in database created date could not be inserted in table same as with insert updated date at the time of update record it will not insert updated date also.  </p>
  
  <p>My code sample is show as below :  </p>
  
  <p>Listener Class :  </p>
  
  <pre><code>public class PreInsertListener implements PreInsertEventListener,  
                                            PreUpdateEventListener 
  {
      @Override
      public boolean onPreInsert(PreInsertEvent arg0) {
          City.class.cast(arg0.getEntity()).setCreated_date(new Date());
          return false;
      }
  
      @Override
      public boolean onPreUpdate(PreUpdateEvent arg0) {
          System.out.println("Update event......");
          City.class.cast(arg0.getEntity()).setUpdated_date(new Date());
          return false;
      }
  }
  </code></pre>
  
  <p>Hibernate Connection class :  </p>
  
  <pre><code>public class HibernateUtil 
  {
  
      private static final SessionFactory sessionFactory;
  
      static {
          try {
              AnnotationConfiguration config = new AnnotationConfiguration();
              config.setListener("pre-insert", new PreInsertListener());
              config.setListener("pre-update", new PreInsertListener());
              sessionFactory = config.configure().buildSessionFactory();;
  
          } catch (Throwable ex) {
              System.err.println("Initial SessionFactory creation failed." + ex);
              throw new ExceptionInInitializerError(ex);
          }
      }
  
      public static SessionFactory getSessionFactory() {
          return sessionFactory;
      }
  }
  </code></pre>
  
  <p>Entity save and Update method in DAO :  </p>
  
  <pre><code>public Long saveCity(String cityName)
  {
      Session session = HibernateUtil.getSessionFactory().openSession();
      Transaction transaction = null;
      Long cityId = null;
      try {
          transaction = session.beginTransaction();
          City city = new City();
          city.setName(cityName);
          cityId = (Long) session.save(city);
          //session.flush();
          transaction.commit();
      } catch (HibernateException e) {
          transaction.rollback();
          e.printStackTrace();
      } finally {
          session.close();
      }
      return cityId;
  }
  
  public void updateCity(Long cityId, String cityName)
  {
      Session session = HibernateUtil.getSessionFactory().openSession();
      Transaction transaction = null;
      try {
          transaction = session.beginTransaction();
          City city = (City) session.get(City.class, cityId);
          city.setName(cityName);
          session.update(city);
          //session.flush();
          transaction.commit();
      } catch (HibernateException e) {
          transaction.rollback();
          e.printStackTrace();
      } finally {
          session.close();
      }
  }
  </code></pre>
  
  <p>My Test Class :</p>
  
  <pre><code>public class Main 
  {
      public static void main(String[] args) {
  
          CityDAO cityDAO = new CityDAO();
  
          long cityId1 = cityDAO.saveCity("New York");
  
          cityDAO.updateCity(cityId1, "Paris");
      }
   }
  </code></pre>
  
  <p>If i used <code>session.flush()</code> than it will insert both date created and updated but updated query is executed every time i call flush method. currently i commented code to call <code>session.flush()</code> method as show in code.</p>
  
  <p>What is the solution to this problem ?   </p>