CreatedUpdatedListener implements PreInsertEventListener, PreUpdateEventListener { @Override public boolean onPreUpdate(final PreUpdateEvent event) { Object entity = event.getEntity(); if (entity instanceof IModifiedDateEntity) { String[] propertyNames = event.getPersister().getEntityMetamodel() .getPropertyNames(); Object[] state = event.getState(); Calendar now = Calendar.getInstance(timeZone); IModifiedDateEntity persistingEntity = (IModifiedDateEntity) entity; persistingEntity.setModifiedDate(now); setValue(state, propertyNames, "modifiedDate", persistingEntity.getModifiedDate()); } return false; }  @Override boolean onPreInsert(final PreInsertEvent event); @Override boolean onPreUpdate(final PreUpdateEvent event);  }
@Test public synchronized void testOnPreUpdate() throws InterruptedException { TestEntity a = new TestEntity(); a.setName("foo"); Session s = getSession(); s.getTransaction().begin(); s.saveOrUpdate(a); s.getTransaction().commit(); s.evict(a); s.getTransaction().begin(); a = s.get(TestEntity.class, a.getId()); s.getTransaction().commit(); Calendar created = a.getCreatedDate(); Calendar modified = a.getModifiedDate(); this.wait(1000); a.setName("bar"); s.getTransaction().begin(); s.saveOrUpdate(a); s.getTransaction().commit(); assertEquals(created, a.getCreatedDate()); assertNotEquals(modified, a.getModifiedDate()); Calendar newModifiedDate = a.getModifiedDate(); s.evict(a); TestEntity testEntity = s.get(TestEntity.class, a.getId()); assertEquals(created, testEntity.getCreatedDate()); assertEquals(newModifiedDate, testEntity.getModifiedDate()); assertNotEquals(modified, testEntity.getModifiedDate()); }