private Object instantiateCollectionPropertyIfAppropriate(
     ResultMapping resultMapping, MetaObject metaObject) {
   final String propertyName = resultMapping.getProperty();
   Object propertyValue = metaObject.getValue(propertyName);
   if (propertyValue == null) {
     Class<?> type = resultMapping.getJavaType();
     if (type == null) {
       type = metaObject.getSetterType(propertyName);
     }
     try {
       if (objectFactory.isCollection(type)) {
         propertyValue = objectFactory.create(type);
         metaObject.setValue(propertyName, propertyValue);
         return propertyValue;
       }
     } catch (Exception e) {
       throw new ExecutorException(
           "Error instantiating collection property for result '"
               + resultMapping.getProperty()
               + "'.  Cause: "
               + e,
           e);
     }
   } else if (objectFactory.isCollection(propertyValue.getClass())) {
     return propertyValue;
   }
   return null;
 }
 private boolean applyAutomaticMappings(
     ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, String columnPrefix)
     throws SQLException {
   final List<String> unmappedColumnNames = rsw.getUnmappedColumnNames(resultMap, columnPrefix);
   boolean foundValues = false;
   for (String columnName : unmappedColumnNames) {
     String propertyName = columnName;
     if (columnPrefix != null && columnPrefix.length() > 0) {
       // When columnPrefix is specified,
       // ignore columns without the prefix.
       if (columnName.toUpperCase(Locale.ENGLISH).startsWith(columnPrefix)) {
         propertyName = columnName.substring(columnPrefix.length());
       } else {
         continue;
       }
     }
     final String property =
         metaObject.findProperty(propertyName, configuration.isMapUnderscoreToCamelCase());
     if (property != null && metaObject.hasSetter(property)) {
       final Class<?> propertyType = metaObject.getSetterType(property);
       if (typeHandlerRegistry.hasTypeHandler(propertyType)) {
         final TypeHandler<?> typeHandler = rsw.getTypeHandler(propertyType, columnName);
         final Object value = typeHandler.getResult(rsw.getResultSet(), columnName);
         if (value != null
             || configuration.isCallSettersOnNulls()) { // issue #377, call setter on nulls
           if (value != null || !propertyType.isPrimitive()) {
             metaObject.setValue(property, value);
           }
           foundValues = true;
         }
       }
     }
   }
   return foundValues;
 }
 private boolean applyPropertyMappings(
     ResultSetWrapper rsw,
     ResultMap resultMap,
     MetaObject metaObject,
     ResultLoaderMap lazyLoader,
     String columnPrefix)
     throws SQLException {
   final List<String> mappedColumnNames = rsw.getMappedColumnNames(resultMap, columnPrefix);
   boolean foundValues = false;
   final List<ResultMapping> propertyMappings = resultMap.getPropertyResultMappings();
   for (ResultMapping propertyMapping : propertyMappings) {
     final String column = prependPrefix(propertyMapping.getColumn(), columnPrefix);
     if (propertyMapping.isCompositeResult()
         || (column != null && mappedColumnNames.contains(column.toUpperCase(Locale.ENGLISH)))
         || propertyMapping.getResultSet() != null) {
       Object value =
           getPropertyMappingValue(
               rsw.getResultSet(), metaObject, propertyMapping, lazyLoader, columnPrefix);
       final String property = propertyMapping.getProperty(); // issue #541 make property optional
       if (value != NO_VALUE
           && property != null
           && (value != null
               || configuration.isCallSettersOnNulls())) { // issue #377, call setter on nulls
         if (value != null || !metaObject.getSetterType(property).isPrimitive()) {
           metaObject.setValue(property, value);
         }
         foundValues = true;
       }
     }
   }
   return foundValues;
 }
Exemple #4
0
 private void setCacheProperties(Cache cache) {
   if (properties != null) {
     MetaObject metaCache = SystemMetaObject.forObject(cache);
     for (Map.Entry<Object, Object> entry : properties.entrySet()) {
       String name = (String) entry.getKey();
       String value = (String) entry.getValue();
       if (metaCache.hasSetter(name)) {
         Class<?> type = metaCache.getSetterType(name);
         if (String.class == type) {
           metaCache.setValue(name, value);
         } else if (int.class == type || Integer.class == type) {
           metaCache.setValue(name, Integer.valueOf(value));
         } else if (long.class == type || Long.class == type) {
           metaCache.setValue(name, Long.valueOf(value));
         } else if (short.class == type || Short.class == type) {
           metaCache.setValue(name, Short.valueOf(value));
         } else if (byte.class == type || Byte.class == type) {
           metaCache.setValue(name, Byte.valueOf(value));
         } else if (float.class == type || Float.class == type) {
           metaCache.setValue(name, Float.valueOf(value));
         } else if (boolean.class == type || Boolean.class == type) {
           metaCache.setValue(name, Boolean.valueOf(value));
         } else if (double.class == type || Double.class == type) {
           metaCache.setValue(name, Double.valueOf(value));
         } else {
           throw new CacheException(
               "Unsupported property type for cache: '" + name + "' of type " + type);
         }
       }
     }
   }
 }
 private Object prepareCompositeKeyParameter(
     ResultSet rs, ResultMapping resultMapping, Class<?> parameterType, String columnPrefix)
     throws SQLException {
   final Object parameterObject = instantiateParameterObject(parameterType);
   final MetaObject metaObject = configuration.newMetaObject(parameterObject);
   boolean foundValues = false;
   for (ResultMapping innerResultMapping : resultMapping.getComposites()) {
     final Class<?> propType = metaObject.getSetterType(innerResultMapping.getProperty());
     final TypeHandler<?> typeHandler = typeHandlerRegistry.getTypeHandler(propType);
     final Object propValue =
         typeHandler.getResult(rs, prependPrefix(innerResultMapping.getColumn(), columnPrefix));
     if (propValue != null) { // issue #353 & #560 do not execute nested query if key is null
       metaObject.setValue(innerResultMapping.getProperty(), propValue);
       foundValues = true;
     }
   }
   return foundValues ? parameterObject : null;
 }
Exemple #6
0
 public void load() {
   Object value = null;
   @SuppressWarnings("unchecked") // we suppose we get back a List
   List<Object> list = (List<Object>) localCache.getObject(key);
   Class<?> targetType = resultObject.getSetterType(property);
   if (targetType.isAssignableFrom(list.getClass())) {
     value = list;
   } else if (objectFactory.isCollection(targetType)) {
     value = objectFactory.create(targetType);
     MetaObject metaObject = configuration.newMetaObject(value);
     metaObject.addAll(list);
   } else if (targetType.isArray()) {
     Object[] array = (Object[]) Array.newInstance(targetType.getComponentType(), list.size());
     value = list.toArray(array);
   } else {
     if (list != null && list.size() > 1) {
       throw new ExecutorException(
           "Statement returned more than one row, where no more than one was expected.");
     } else if (list != null && list.size() == 1) {
       value = list.get(0);
     }
   }
   resultObject.setValue(property, value);
 }