CreatedUpdatedListener implements PreInsertEventListener, PreUpdateEventListener { @Override public boolean onPreInsert(final PreInsertEvent event) { Object entity = event.getEntity(); Object[] state = event.getState(); Calendar now = Calendar.getInstance(timeZone); if (entity instanceof ICreatedDateEntity) { String[] propertyNames = event.getPersister().getEntityMetamodel() .getPropertyNames(); ICreatedDateEntity persistingEntity = (ICreatedDateEntity) entity; persistingEntity.setCreatedDate(now); setValue(state, propertyNames, "createdDate", persistingEntity.getCreatedDate()); } if (entity instanceof IModifiedDateEntity) { String[] propertyNames = event.getPersister().getEntityMetamodel() .getPropertyNames(); IModifiedDateEntity modifiedEntity = (IModifiedDateEntity) entity; modifiedEntity.setModifiedDate(now); setValue(state, propertyNames, "modifiedDate", modifiedEntity.getModifiedDate()); } return false; }  @Override boolean onPreInsert(final PreInsertEvent event); @Override boolean onPreUpdate(final PreUpdateEvent event);  }
@Test public void testOnPreInsert() { TestEntity a = new TestEntity(); a.setName("foo"); assertNull(a.getCreatedDate()); assertNull(a.getModifiedDate()); Session s = getSession(); s.getTransaction().begin(); s.saveOrUpdate(a); s.getTransaction().commit(); assertNotNull(a.getCreatedDate()); assertNotNull(a.getModifiedDate()); s.evict(a); TestEntity testEntity = s.get(TestEntity.class, a.getId()); assertNotNull(testEntity.getCreatedDate()); assertNotNull(testEntity.getModifiedDate()); }