google Analytics

Thursday, July 8, 2010

Hibernate EmptyInterceptor Example

/**
* @author NathA1
*/
public class EntryInterceptor extends EmptyInterceptor {
/*
* @see org.hibernate.EmptyInterceptor#onSave(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
*/
@Override
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
final Date now = new Date();
final String loginUserName ="NathA1";
setValue(state, propertyNames, "datecreation", now);
setValue(state, propertyNames, "usercreation", loginUserName);
setValue(state, propertyNames, "dateupd", now);
setValue(state, propertyNames, "userupd", loginUserName);
return true;
}

/*
* @see org.hibernate.EmptyInterceptor#onFlushDirty(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
*/
@Override
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
final Date now = new Date();
final String loginUserName ="NathA1";
setValue(currentState, propertyNames, "dateupd", now);
setValue(currentState, propertyNames, "userupd", loginUserName);
return true;
}

/**
* Sets the value.
*
* @param currentState the current state
* @param propertyNames the property names
* @param propertyToSet the property to set
* @param value the value
*/
private void setValue(Object[] currentState, String[] propertyNames, String propertyToSet, Object value) {
for (int i = 0; i < propertyNames.length; i++) {
if (propertyNames[i].equalsIgnoreCase(propertyToSet)) {
currentState[i] = value;
}
}
}

}

No comments:

Post a Comment