|
32 | 32 | import org.springframework.beans.BeanWrapper;
|
33 | 33 | import org.springframework.beans.NotWritablePropertyException;
|
34 | 34 | import org.springframework.beans.PropertyAccessorFactory;
|
| 35 | +import org.springframework.beans.TypeMismatchException; |
35 | 36 | import org.springframework.dao.DataRetrievalFailureException;
|
36 | 37 | import org.springframework.dao.InvalidDataAccessApiUsageException;
|
37 | 38 | import org.springframework.jdbc.support.JdbcUtils;
|
|
54 | 55 | * <p>To facilitate mapping between columns and fields that don't have matching names,
|
55 | 56 | * try using column aliases in the SQL statement like "select fname as first_name from customer".
|
56 | 57 | *
|
| 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 | + * |
57 | 63 | * <p>Please note that this class is designed to provide convenience rather than high performance.
|
58 | 64 | * For best performance consider using a custom RowMapper.
|
59 | 65 | *
|
@@ -219,7 +225,18 @@ public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
|
219 | 225 | logger.debug("Mapping column '" + column + "' to property '" +
|
220 | 226 | pd.getName() + "' of type " + pd.getPropertyType());
|
221 | 227 | }
|
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 | + } |
223 | 240 | if (populatedProperties != null) {
|
224 | 241 | populatedProperties.add(pd.getName());
|
225 | 242 | }
|
|
0 commit comments