BeanPropertyRowMapper logs in case of no corresponding property as well

Issue: SPR-13323
This commit is contained in:
Juergen Hoeller 2015-08-25 17:54:26 +02:00
parent 74e6213baf
commit 35e1eca9e8
1 changed files with 22 additions and 9 deletions

View File

@ -38,6 +38,7 @@ import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.jdbc.support.JdbcUtils; import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
@ -257,22 +258,27 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
for (int index = 1; index <= columnCount; index++) { for (int index = 1; index <= columnCount; index++) {
String column = JdbcUtils.lookupColumnName(rsmd, index); String column = JdbcUtils.lookupColumnName(rsmd, index);
PropertyDescriptor pd = this.mappedFields.get(lowerCaseName(column.replaceAll(" ", ""))); String field = lowerCaseName(column.replaceAll(" ", ""));
PropertyDescriptor pd = this.mappedFields.get(field);
if (pd != null) { if (pd != null) {
try { try {
Object value = getColumnValue(rs, index, pd); Object value = getColumnValue(rs, index, pd);
if (logger.isDebugEnabled() && rowNumber == 0) { if (rowNumber == 0 && logger.isDebugEnabled()) {
logger.debug("Mapping column '" + column + "' to property '" + logger.debug("Mapping column '" + column + "' to property '" + pd.getName() +
pd.getName() + "' of type " + pd.getPropertyType()); "' of type [" + ClassUtils.getQualifiedName(pd.getPropertyType()) + "]");
} }
try { try {
bw.setPropertyValue(pd.getName(), value); bw.setPropertyValue(pd.getName(), value);
} }
catch (TypeMismatchException ex) { catch (TypeMismatchException ex) {
if (value == null && this.primitivesDefaultedForNullValue) { if (value == null && this.primitivesDefaultedForNullValue) {
logger.debug("Intercepted TypeMismatchException for row " + rowNumber + " and column '" + if (logger.isDebugEnabled()) {
column + "' with null value when setting property '" + pd.getName() + logger.debug("Intercepted TypeMismatchException for row " + rowNumber +
"' of type " + pd.getPropertyType() + " on object: " + mappedObject); " and column '" + column + "' with null value when setting property '" +
pd.getName() + "' of type [" +
ClassUtils.getQualifiedName(pd.getPropertyType()) +
"] on object: " + mappedObject, ex);
}
} }
else { else {
throw ex; throw ex;
@ -284,14 +290,21 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
} }
catch (NotWritablePropertyException ex) { catch (NotWritablePropertyException ex) {
throw new DataRetrievalFailureException( throw new DataRetrievalFailureException(
"Unable to map column " + column + " to property " + pd.getName(), ex); "Unable to map column '" + column + "' to property '" + pd.getName() + "'", ex);
}
}
else {
// No PropertyDescriptor found
if (rowNumber == 0 && logger.isDebugEnabled()) {
logger.debug("No property found for column '" + column + "' mapped to field '" + field + "'");
} }
} }
} }
if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) { if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) {
throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields " + throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields " +
"necessary to populate object of class [" + this.mappedClass + "]: " + this.mappedProperties); "necessary to populate object of class [" + this.mappedClass.getName() + "]: " +
this.mappedProperties);
} }
return mappedObject; return mappedObject;