private void createRowKeyForUnmappedProperties( ResultMap resultMap, ResultSetWrapper rsw, CacheKey cacheKey, String columnPrefix) throws SQLException { final MetaClass metaType = MetaClass.forClass(resultMap.getType()); List<String> unmappedColumnNames = rsw.getUnmappedColumnNames(resultMap, columnPrefix); for (String column : unmappedColumnNames) { String property = column; if (columnPrefix != null && columnPrefix.length() > 0) { // When columnPrefix is specified, // ignore columns without the prefix. if (column.toUpperCase(Locale.ENGLISH).startsWith(columnPrefix)) { property = column.substring(columnPrefix.length()); } else { continue; } } if (metaType.findProperty(property, configuration.isMapUnderscoreToCamelCase()) != null) { String value = rsw.getResultSet().getString(column); if (value != null) { cacheKey.update(column); cacheKey.update(value); } } } }
public static <T> T populate( Configuration configuration, Class<?> entityClass, T entity, Map<String, Object> resultMap, boolean isMapUnderscoreToCamelCase) { MetaClass metaClass = MetaClass.forClass(entityClass, configuration.getReflectorFactory()); for (String key : resultMap.keySet()) { String property = metaClass.findProperty(key, isMapUnderscoreToCamelCase); if (property == null) { property = metaClass.findProperty( key.replace("_id", ""), isMapUnderscoreToCamelCase); // just for association entity property if (property == null) { property = getPropertyNameForAssociationEntity(entityClass, metaClass, key); } } if (property != null && metaClass.hasSetter(property)) { Object value = resultMap.get(key); try { Method getterMethod = findGetterMethod(property, entityClass); // Handle the property that annotated by {#link Enumerated}. Enumerated enumrated = AnnotationUtils.findAnnotation(getterMethod, Enumerated.class); if (enumrated != null) { Class<?> returnType = getterMethod.getReturnType(); if (enumrated.value() == EnumType.STRING) { value = Enum.valueOf(returnType.asSubclass(Enum.class), String.valueOf(value)); } else { value = returnType.asSubclass(Enum.class).getEnumConstants()[(int) value]; } } // Handler the property that annotated by {#link Association} Association association = AnnotationUtils.findAnnotation(getterMethod, Association.class); if (association != null) { Class<?> returnType = getterMethod.getReturnType(); MetaClass associationMetaClass = MetaClass.forClass(returnType, configuration.getReflectorFactory()); Invoker m = associationMetaClass.getSetInvoker("id"); Object associationEntity = returnType.newInstance(); Object[] params = {value}; m.invoke(associationEntity, params); value = associationEntity; } Invoker method = metaClass.getSetInvoker(property); Object[] params = {value}; try { method.invoke(entity, params); } catch (Throwable t) { throw ExceptionUtil.unwrapThrowable(t); } } catch (Throwable t) { throw new ReflectionException( "Could not set property '" + property + "' of '" + entityClass + "' with value '" + value + "' Cause: " + t.toString(), t); } } } return entity; }