List of usage examples for org.hibernate.event.spi EventSource load
<T> T load(Class<T> theClass, Serializable id);
From source file:org.egov.infra.config.persistence.event.listener.HibernateEventListener.java
License:Open Source License
/** * For new objects that are created, this event is used to set the audit properties. This is done here instead of the * pre-insert event because Hibernate checks for not-null constraints before the pre-update and pre-insert are fired. *///from w w w. j a va 2 s . c o m @Override public void onSaveOrUpdate(final SaveOrUpdateEvent event) { final EventSource session = event.getSession(); final Object object = event.getObject(); if (object instanceof BaseModel && !session.getPersistenceContext().reassociateIfUninitializedProxy(object)) { // only update the entity if it has been changed final Date currentDate = new Date(); final User usr = session.load(User.class, ApplicationThreadLocals.getUserId()); final BaseModel entity = (BaseModel) session.getPersistenceContext().unproxyAndReassociate(object); if (entity.getCreatedBy() == null) { entity.setCreatedDate(currentDate); entity.setCreatedBy(usr); entity.setModifiedBy(usr); entity.setModifiedDate(currentDate); } } else if (object instanceof Auditable && !session.getPersistenceContext().reassociateIfUninitializedProxy(object)) { final User usr = session.load(User.class, ApplicationThreadLocals.getUserId()); final AbstractAuditable entity = (AbstractAuditable) session.getPersistenceContext() .unproxyAndReassociate(object); if (entity.getCreatedBy() == null) { final Date currentDate = new Date(); entity.setCreatedDate(currentDate); entity.setCreatedBy(usr); entity.setLastModifiedBy(usr); entity.setLastModifiedDate(currentDate); } } }