Search in sources :

Example 11 with ResultMapping

use of org.apache.ibatis.mapping.ResultMapping in project mybatis-3 by mybatis.

the class DefaultResultSetHandler method createRowKey.

//
// UNIQUE RESULT KEY
//
private CacheKey createRowKey(ResultMap resultMap, ResultSetWrapper rsw, String columnPrefix) throws SQLException {
    final CacheKey cacheKey = new CacheKey();
    cacheKey.update(resultMap.getId());
    List<ResultMapping> resultMappings = getResultMappingsForRowKey(resultMap);
    if (resultMappings.size() == 0) {
        if (Map.class.isAssignableFrom(resultMap.getType())) {
            createRowKeyForMap(rsw, cacheKey);
        } else {
            createRowKeyForUnmappedProperties(resultMap, rsw, cacheKey, columnPrefix);
        }
    } else {
        createRowKeyForMappedProperties(resultMap, rsw, cacheKey, resultMappings, columnPrefix);
    }
    if (cacheKey.getUpdateCount() < 2) {
        return CacheKey.NULL_CACHE_KEY;
    }
    return cacheKey;
}
Also used : ResultMapping(org.apache.ibatis.mapping.ResultMapping) CacheKey(org.apache.ibatis.cache.CacheKey)

Example 12 with ResultMapping

use of org.apache.ibatis.mapping.ResultMapping in project mybatis-3 by mybatis.

the class DefaultResultSetHandler method addPendingChildRelation.

private void addPendingChildRelation(ResultSet rs, MetaObject metaResultObject, ResultMapping parentMapping) throws SQLException {
    CacheKey cacheKey = createKeyForMultipleResults(rs, parentMapping, parentMapping.getColumn(), parentMapping.getColumn());
    PendingRelation deferLoad = new PendingRelation();
    deferLoad.metaObject = metaResultObject;
    deferLoad.propertyMapping = parentMapping;
    List<PendingRelation> relations = pendingRelations.get(cacheKey);
    // issue #255
    if (relations == null) {
        relations = new ArrayList<DefaultResultSetHandler.PendingRelation>();
        pendingRelations.put(cacheKey, relations);
    }
    relations.add(deferLoad);
    ResultMapping previous = nextResultMaps.get(parentMapping.getResultSet());
    if (previous == null) {
        nextResultMaps.put(parentMapping.getResultSet(), parentMapping);
    } else {
        if (!previous.equals(parentMapping)) {
            throw new ExecutorException("Two different properties are mapped to the same resultSet");
        }
    }
}
Also used : ExecutorException(org.apache.ibatis.executor.ExecutorException) ResultMapping(org.apache.ibatis.mapping.ResultMapping) CacheKey(org.apache.ibatis.cache.CacheKey)

Example 13 with ResultMapping

use of org.apache.ibatis.mapping.ResultMapping in project mybatis-3 by mybatis.

the class DefaultResultSetHandler method createPrimitiveResultObject.

private Object createPrimitiveResultObject(ResultSetWrapper rsw, ResultMap resultMap, String columnPrefix) throws SQLException {
    final Class<?> resultType = resultMap.getType();
    final String columnName;
    if (!resultMap.getResultMappings().isEmpty()) {
        final List<ResultMapping> resultMappingList = resultMap.getResultMappings();
        final ResultMapping mapping = resultMappingList.get(0);
        columnName = prependPrefix(mapping.getColumn(), columnPrefix);
    } else {
        columnName = rsw.getColumnNames().get(0);
    }
    final TypeHandler<?> typeHandler = rsw.getTypeHandler(resultType, columnName);
    return typeHandler.getResult(rsw.getResultSet(), columnName);
}
Also used : ResultMapping(org.apache.ibatis.mapping.ResultMapping)

Example 14 with ResultMapping

use of org.apache.ibatis.mapping.ResultMapping in project mybatis-3 by mybatis.

the class DefaultResultSetHandler method createResultObject.

//
// INSTANTIATION & CONSTRUCTOR MAPPING
//
private Object createResultObject(ResultSetWrapper rsw, ResultMap resultMap, ResultLoaderMap lazyLoader, String columnPrefix) throws SQLException {
    // reset previous mapping result
    this.useConstructorMappings = false;
    final List<Class<?>> constructorArgTypes = new ArrayList<Class<?>>();
    final List<Object> constructorArgs = new ArrayList<Object>();
    Object resultObject = createResultObject(rsw, resultMap, constructorArgTypes, constructorArgs, columnPrefix);
    if (resultObject != null && !hasTypeHandlerForResultObject(rsw, resultMap.getType())) {
        final List<ResultMapping> propertyMappings = resultMap.getPropertyResultMappings();
        for (ResultMapping propertyMapping : propertyMappings) {
            // issue gcode #109 && issue #149
            if (propertyMapping.getNestedQueryId() != null && propertyMapping.isLazy()) {
                resultObject = configuration.getProxyFactory().createProxy(resultObject, lazyLoader, configuration, objectFactory, constructorArgTypes, constructorArgs);
                break;
            }
        }
    }
    // set current mapping result
    this.useConstructorMappings = (resultObject != null && !constructorArgTypes.isEmpty());
    return resultObject;
}
Also used : ResultMapping(org.apache.ibatis.mapping.ResultMapping) ArrayList(java.util.ArrayList) MetaClass(org.apache.ibatis.reflection.MetaClass) MetaObject(org.apache.ibatis.reflection.MetaObject)

Example 15 with ResultMapping

use of org.apache.ibatis.mapping.ResultMapping in project mybatis-3 by mybatis.

the class DefaultResultSetHandler method applyNestedResultMappings.

//
// NESTED RESULT MAP (JOIN MAPPING)
//
private boolean applyNestedResultMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, String parentPrefix, CacheKey parentRowKey, boolean newObject) {
    boolean foundValues = false;
    for (ResultMapping resultMapping : resultMap.getPropertyResultMappings()) {
        final String nestedResultMapId = resultMapping.getNestedResultMapId();
        if (nestedResultMapId != null && resultMapping.getResultSet() == null) {
            try {
                final String columnPrefix = getColumnPrefix(parentPrefix, resultMapping);
                final ResultMap nestedResultMap = getNestedResultMap(rsw.getResultSet(), nestedResultMapId, columnPrefix);
                if (resultMapping.getColumnPrefix() == null) {
                    // try to fill circular reference only when columnPrefix
                    // is not specified for the nested result map (issue #215)
                    Object ancestorObject = ancestorObjects.get(nestedResultMapId);
                    if (ancestorObject != null) {
                        if (newObject) {
                            // issue #385
                            linkObjects(metaObject, resultMapping, ancestorObject);
                        }
                        continue;
                    }
                }
                final CacheKey rowKey = createRowKey(nestedResultMap, rsw, columnPrefix);
                final CacheKey combinedKey = combineKeys(rowKey, parentRowKey);
                Object rowValue = nestedResultObjects.get(combinedKey);
                boolean knownValue = (rowValue != null);
                // mandatory
                instantiateCollectionPropertyIfAppropriate(resultMapping, metaObject);
                if (anyNotNullColumnHasValue(resultMapping, columnPrefix, rsw)) {
                    rowValue = getRowValue(rsw, nestedResultMap, combinedKey, columnPrefix, rowValue);
                    if (rowValue != null && !knownValue) {
                        linkObjects(metaObject, resultMapping, rowValue);
                        foundValues = true;
                    }
                }
            } catch (SQLException e) {
                throw new ExecutorException("Error getting nested result map values for '" + resultMapping.getProperty() + "'.  Cause: " + e, e);
            }
        }
    }
    return foundValues;
}
Also used : ResultMap(org.apache.ibatis.mapping.ResultMap) ExecutorException(org.apache.ibatis.executor.ExecutorException) SQLException(java.sql.SQLException) ResultMapping(org.apache.ibatis.mapping.ResultMapping) MetaObject(org.apache.ibatis.reflection.MetaObject) CacheKey(org.apache.ibatis.cache.CacheKey)

Aggregations

ResultMapping (org.apache.ibatis.mapping.ResultMapping)29 ArrayList (java.util.ArrayList)17 ResultMap (org.apache.ibatis.mapping.ResultMap)11 ResultFlag (org.apache.ibatis.mapping.ResultFlag)9 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)7 MappedStatement (org.apache.ibatis.mapping.MappedStatement)7 MetaObject (org.apache.ibatis.reflection.MetaObject)7 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)7 ParameterMap (org.apache.ibatis.mapping.ParameterMap)6 TypeHandler (org.apache.ibatis.type.TypeHandler)6 ExecutorException (org.apache.ibatis.executor.ExecutorException)5 SqlSource (org.apache.ibatis.mapping.SqlSource)5 DynamicSqlSource (org.apache.ibatis.scripting.xmltags.DynamicSqlSource)5 List (java.util.List)4 Blog (org.apache.ibatis.domain.blog.Blog)4 SQLException (java.sql.SQLException)3 Date (java.util.Date)3 CacheKey (org.apache.ibatis.cache.CacheKey)3 Author (org.apache.ibatis.domain.blog.Author)3 Comment (org.apache.ibatis.domain.blog.Comment)3