I saw the solution right after posting the question. In BaseTypeHandler code:
[...]
public T getResult(ResultSet rs, String columnName) throws SQLException {
T result = getNullableResult(rs, columnName);
if (rs.wasNull()) {
return null;
} else {
return result;
}
}
public T getResult(ResultSet rs, int columnIndex) throws SQLException {
T result = getNullableResult(rs, columnIndex);
if (rs.wasNull()) {
return null;
} else {
return result;
}
}
public T getResult(CallableStatement cs, int columnIndex) throws SQLException {
T result = getNullableResult(cs, columnIndex);
if (cs.wasNull()) {
return null;
} else {
return result;
}
}
public abstract void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException;
public abstract T getNullableResult(ResultSet rs, String columnName) throws SQLException;
public abstract T getNullableResult(ResultSet rs, int columnIndex) throws SQLException;
public abstract T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException;
[...]
Clearly, BaseTypeHandler
is going to return null
when the value in the database is null
, as cs.WasNull()
is true
in that case. Therefore, the solution is to create a concrete TypeHandler<T>
returning the appropiate implementation (in this case, a NullObject implmentation when the value in the database is null) without subclassing BaseTypeHandler
.
EDITED (folowing quux00's comment): Of course, we can just override getResult
method of BaseTypeHandler
, given that the functionality provided by its method setParameters
can be useful.