Search in sources :

Example 1 with NotWritablePropertyException

use of cn.taketoday.beans.NotWritablePropertyException in project today-infrastructure by TAKETODAY.

the class BeanPropertyRowMapper method mapRow.

/**
 * Extract the values for all columns in the current row.
 * <p>Utilizes public setters and result set meta-data.
 *
 * @see ResultSetMetaData
 */
@Override
public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
    BeanWrapperImpl beanWrapper = this.beanWrapper;
    T mappedObject = constructMappedInstance(rs, beanWrapper);
    beanWrapper.setBeanInstance(mappedObject);
    ResultSetMetaData rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();
    Set<String> populatedProperties = isCheckFullyPopulated() ? new HashSet<>() : null;
    HashMap<String, BeanProperty> mappedFields = this.mappedFields;
    for (int index = 1; index <= columnCount; index++) {
        String column = JdbcUtils.lookupColumnName(rsmd, index);
        String field = lowerCaseName(StringUtils.delete(column, " "));
        BeanProperty property = mappedFields != null ? mappedFields.get(field) : null;
        if (property != null) {
            try {
                // TODO using TypeHandler
                Object value = getColumnValue(rs, index, property);
                if (rowNumber == 0 && debugEnabled) {
                    log.debug("Mapping column '{}' to property '{}' of type '{}'", column, property.getName(), ClassUtils.getQualifiedName(property.getType()));
                }
                try {
                    beanWrapper.setPropertyValue(property.getName(), value);
                } catch (TypeMismatchException ex) {
                    if (value == null && this.primitivesDefaultedForNullValue) {
                        if (debugEnabled) {
                            log.debug("Intercepted TypeMismatchException for row {} and column '{}'" + " with null value when setting property '{}' of type '{}' on object: {}", rowNumber, column, property.getName(), ClassUtils.getQualifiedName(property.getType()), mappedObject, ex);
                        }
                    } else {
                        throw ex;
                    }
                }
                if (populatedProperties != null) {
                    populatedProperties.add(property.getName());
                }
            } catch (NotWritablePropertyException ex) {
                throw new DataRetrievalFailureException("Unable to map column '" + column + "' to property '" + property.getName() + "'", ex);
            }
        } else {
            // No BeanProperty found
            if (rowNumber == 0 && debugEnabled) {
                log.debug("No property found for column '{}' mapped to field '{}'", column, field);
            }
        }
    }
    if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) {
        throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields " + "necessary to populate object of " + this.mappedClass + ": " + this.mappedProperties);
    }
    return mappedObject;
}
Also used : BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl) TypeMismatchException(cn.taketoday.beans.TypeMismatchException) BeanProperty(cn.taketoday.beans.BeanProperty) ResultSetMetaData(java.sql.ResultSetMetaData) NotWritablePropertyException(cn.taketoday.beans.NotWritablePropertyException) InvalidDataAccessApiUsageException(cn.taketoday.dao.InvalidDataAccessApiUsageException) DataRetrievalFailureException(cn.taketoday.dao.DataRetrievalFailureException)

Example 2 with NotWritablePropertyException

use of cn.taketoday.beans.NotWritablePropertyException in project today-framework by TAKETODAY.

the class BeanPropertyRowMapper method mapRow.

/**
 * Extract the values for all columns in the current row.
 * <p>Utilizes public setters and result set meta-data.
 *
 * @see ResultSetMetaData
 */
@Override
public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
    BeanWrapperImpl beanWrapper = this.beanWrapper;
    T mappedObject = constructMappedInstance(rs, beanWrapper);
    beanWrapper.setBeanInstance(mappedObject);
    ResultSetMetaData rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();
    Set<String> populatedProperties = isCheckFullyPopulated() ? new HashSet<>() : null;
    HashMap<String, BeanProperty> mappedFields = this.mappedFields;
    for (int index = 1; index <= columnCount; index++) {
        String column = JdbcUtils.lookupColumnName(rsmd, index);
        String field = lowerCaseName(StringUtils.delete(column, " "));
        BeanProperty property = mappedFields != null ? mappedFields.get(field) : null;
        if (property != null) {
            try {
                // TODO using TypeHandler
                Object value = getColumnValue(rs, index, property);
                if (rowNumber == 0 && debugEnabled) {
                    log.debug("Mapping column '{}' to property '{}' of type '{}'", column, property.getName(), ClassUtils.getQualifiedName(property.getType()));
                }
                try {
                    beanWrapper.setPropertyValue(property.getName(), value);
                } catch (TypeMismatchException ex) {
                    if (value == null && this.primitivesDefaultedForNullValue) {
                        if (debugEnabled) {
                            log.debug("Intercepted TypeMismatchException for row {} and column '{}'" + " with null value when setting property '{}' of type '{}' on object: {}", rowNumber, column, property.getName(), ClassUtils.getQualifiedName(property.getType()), mappedObject, ex);
                        }
                    } else {
                        throw ex;
                    }
                }
                if (populatedProperties != null) {
                    populatedProperties.add(property.getName());
                }
            } catch (NotWritablePropertyException ex) {
                throw new DataRetrievalFailureException("Unable to map column '" + column + "' to property '" + property.getName() + "'", ex);
            }
        } else {
            // No BeanProperty found
            if (rowNumber == 0 && debugEnabled) {
                log.debug("No property found for column '{}' mapped to field '{}'", column, field);
            }
        }
    }
    if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) {
        throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields " + "necessary to populate object of " + this.mappedClass + ": " + this.mappedProperties);
    }
    return mappedObject;
}
Also used : BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl) TypeMismatchException(cn.taketoday.beans.TypeMismatchException) BeanProperty(cn.taketoday.beans.BeanProperty) ResultSetMetaData(java.sql.ResultSetMetaData) NotWritablePropertyException(cn.taketoday.beans.NotWritablePropertyException) InvalidDataAccessApiUsageException(cn.taketoday.dao.InvalidDataAccessApiUsageException) DataRetrievalFailureException(cn.taketoday.dao.DataRetrievalFailureException)

Aggregations

BeanProperty (cn.taketoday.beans.BeanProperty)2 BeanWrapperImpl (cn.taketoday.beans.BeanWrapperImpl)2 NotWritablePropertyException (cn.taketoday.beans.NotWritablePropertyException)2 TypeMismatchException (cn.taketoday.beans.TypeMismatchException)2 DataRetrievalFailureException (cn.taketoday.dao.DataRetrievalFailureException)2 InvalidDataAccessApiUsageException (cn.taketoday.dao.InvalidDataAccessApiUsageException)2 ResultSetMetaData (java.sql.ResultSetMetaData)2