Skip to content

Commit 360434d

Browse files
author
Thomas Risberg
committed
defaulting primitive property when receiving null value from result in BeanPropertyRowMapper (SPR-5588)
1 parent 665b284 commit 360434d

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/BeanPropertyRowMapper.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.beans.BeanWrapper;
3333
import org.springframework.beans.NotWritablePropertyException;
3434
import org.springframework.beans.PropertyAccessorFactory;
35+
import org.springframework.beans.TypeMismatchException;
3536
import org.springframework.dao.DataRetrievalFailureException;
3637
import org.springframework.dao.InvalidDataAccessApiUsageException;
3738
import org.springframework.jdbc.support.JdbcUtils;
@@ -54,6 +55,11 @@
5455
* <p>To facilitate mapping between columns and fields that don't have matching names,
5556
* try using column aliases in the SQL statement like "select fname as first_name from customer".
5657
*
58+
* <p>For 'null' values read from the databasem, we will attempt to call the setter, but in the case of
59+
* primitives, this causes a TypeMismatchException. We will trap this exception and log a warning message.
60+
* Be aware that if you use the values from the generated bean to update the database the primitive value
61+
* will have been set to the primitive's default value instead of null.
62+
*
5763
* <p>Please note that this class is designed to provide convenience rather than high performance.
5864
* For best performance consider using a custom RowMapper.
5965
*
@@ -219,7 +225,18 @@ public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
219225
logger.debug("Mapping column '" + column + "' to property '" +
220226
pd.getName() + "' of type " + pd.getPropertyType());
221227
}
222-
bw.setPropertyValue(pd.getName(), value);
228+
try {
229+
bw.setPropertyValue(pd.getName(), value);
230+
}
231+
catch (TypeMismatchException e) {
232+
logger.warn("Intercepted TypeMismatchException for row " + rowNumber +
233+
" and column '" + column + "' with value " + value +
234+
" when setting property '" + pd.getName() + "' of type " + pd.getPropertyType() +
235+
" on object: " + mappedObject);
236+
if (value != null) {
237+
throw e;
238+
}
239+
}
223240
if (populatedProperties != null) {
224241
populatedProperties.add(pd.getName());
225242
}

0 commit comments

Comments
 (0)