You could possibly implement an Interceptor and override the onLoad
Method: More Information on Hibernate Interceptors Here
I have used this strategy overriding the onFlushDirty and onSave Methods to update Timestamp fields. I have never used it during onLoad
.
Here is an example Interceptor based on a similar one I have used (Not Tested):
public final class PepperInterceptor extends EmptyInterceptor {
/** The Value to set as the Pepper field when loading from the DB. */
private static final String DEFAULT_PEPPER = "[PROTECTED-PEPPER]";
@Override
public boolean onLoad(final Object entity, final Serializable id, final Object[] currentState,
final Object[] previousState, final String[] propertyNames, final Type[] types) {
if (entity instanceof User) {
for (int i = 0; i < propertyNames.length; i++) {
if (propertyNames[i].equalsIgnoreCase("pepper")) {
currentState[i] = DEFAULT_PEPPER;
}
}
return true;
}
return super.onLoad(entity, id, currentState, previousState, propertyNames, types);
}
}
EDIT:
Thinking about this further, you would have to ensure the value is never overwritten with the DEFAULT_PEPPER
, possibly using something like updatable=false
in the column definition of your Entity Class