An interceptor is the way to go if you want to change the behavior for all lines. Oracle internally treats the empty string as null ( '' is null returns true), but this may not be true for other databases. For them, you must override the onFlushDirty and onSave , and also not use null in the database (or always use them if you want):
public class MPSessionInterceptor extends EmptyInterceptor { @Override public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) { return convertEmptyStrings(currentState, types); } @Override public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { return convertEmptyStrings(state, types); } @Override public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { return convertEmptyStrings(state, types); } private boolean convertEmptyStrings(Object[] state, Type[] types) {
You can contact Hibernate to use an interceptor.
source share