Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm checking Pascal's answer as the correct answer as he pointed me directly to the point in the documentation that provided detail and example code. But for the reader, I'm adding more detail here.</p> <p>I tried both the hibernate events as well as the interceptors, and found that an interceptor worked better in my situation. It was relatively easy to implement. </p> <p>Thanks for the help!</p> <p>Below is the code for the interceptor:</p> <pre><code>public class AuditInterceptor extends EmptyInterceptor { private Log log = LogFactory.getLog(this.getClass()); private int updates; private int creates; @Override public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { if (entity instanceof AuditableVO) { creates++; // Find the create date and change it for (int i=0; i &lt; propertyNames.length; i++) { if (propertyNames[i].equals("createDate")) { state[i] = new Date(); return true; } } } return false; } @Override public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) { if (entity instanceof AuditableVO) { updates++; // Find the update date and change it for (int i=0; i &lt; propertyNames.length; i++) { if (propertyNames[i].equals("updateDate")) { currentState[i] = new Date(); return true; } } } return false; } @Override public void afterTransactionCompletion(Transaction tx) { if (tx.wasCommitted()) { log.info("Creations: " + creates + ", Updates: " + updates); } creates = 0; updates = 0; super.afterTransactionCompletion(tx); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload