Question on IntegerTypeHandler behaviour

7 views
Skip to first unread message

Asaf Mesika

unread,
Aug 1, 2019, 7:13:26 AM8/1/19
to mybatis-user
Hi,

I've noticed that there is an odd behavior (at least from my point of view) in IntegerTypeHandler class:


@Override
public Integer getNullableResult(ResultSet rs, String columnName)
throws SQLException {
return rs.getInt(columnName);
}

rs.getInt(columnName) returns 0 when the column value is null:

/**
* Get the value of a column in the current row as a Java int.
*
* @param columnIndex
* the first column is 1, the second is 2,...
*
* @return the column value; 0 if SQL NULL
*
* @exception SQLException
* if a database access error occurs
*/
public int getInt(int columnIndex) throws SQLException {

This is taken from com.mysql.jdbc.ResultSetImpl.


Why isn't the implementation for getNullableResult was something like:

int result = rs.getInt(columnName);
if (rs.wasNull()) {
   return null;
} else {
   return result;
}

This will make IntegerTypeHandler support Nullable Integer columns, and not return the number 0 instead of NULL.



Iwao AVE!

unread,
Aug 1, 2019, 7:57:34 AM8/1/19
to mybatis-user
Hi Asaf,

That source looks old (< 3.5.0), so there should be a wasNull() check in BaseTypeHandler#getResult() method.
We moved the wasNull() check to IntegerTypeHandler in 3.5.0 [1].
In any version, IntegerTypeHandler does not return 0 if the column 'was null'.

Regards,
Iwao

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/93e8cd6e-19bb-409b-b973-c21d118cff2d%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages