Search in sources :

Example 1 with ExecutorException

use of org.apache.ibatis.executor.ExecutorException in project mybatis-3 by mybatis.

the class DefaultResultSetHandler method createResultObject.

private Object createResultObject(ResultSetWrapper rsw, ResultMap resultMap, List<Class<?>> constructorArgTypes, List<Object> constructorArgs, String columnPrefix) throws SQLException {
    final Class<?> resultType = resultMap.getType();
    final MetaClass metaType = MetaClass.forClass(resultType, reflectorFactory);
    final List<ResultMapping> constructorMappings = resultMap.getConstructorResultMappings();
    if (hasTypeHandlerForResultObject(rsw, resultType)) {
        return createPrimitiveResultObject(rsw, resultMap, columnPrefix);
    } else if (!constructorMappings.isEmpty()) {
        return createParameterizedResultObject(rsw, resultType, constructorMappings, constructorArgTypes, constructorArgs, columnPrefix);
    } else if (resultType.isInterface() || metaType.hasDefaultConstructor()) {
        return objectFactory.create(resultType);
    } else if (shouldApplyAutomaticMappings(resultMap, false)) {
        return createByConstructorSignature(rsw, resultType, constructorArgTypes, constructorArgs, columnPrefix);
    }
    throw new ExecutorException("Do not know how to create an instance of " + resultType);
}
Also used : ExecutorException(org.apache.ibatis.executor.ExecutorException) MetaClass(org.apache.ibatis.reflection.MetaClass) ResultMapping(org.apache.ibatis.mapping.ResultMapping)

Example 2 with ExecutorException

use of org.apache.ibatis.executor.ExecutorException in project mybatis-3 by mybatis.

the class Jdbc3KeyGenerator method processBatch.

public void processBatch(MappedStatement ms, Statement stmt, Collection<Object> parameters) {
    ResultSet rs = null;
    try {
        rs = stmt.getGeneratedKeys();
        final Configuration configuration = ms.getConfiguration();
        final TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
        final String[] keyProperties = ms.getKeyProperties();
        final ResultSetMetaData rsmd = rs.getMetaData();
        TypeHandler<?>[] typeHandlers = null;
        if (keyProperties != null && rsmd.getColumnCount() >= keyProperties.length) {
            for (Object parameter : parameters) {
                // there should be one row for each statement (also one for each parameter)
                if (!rs.next()) {
                    break;
                }
                final MetaObject metaParam = configuration.newMetaObject(parameter);
                if (typeHandlers == null) {
                    typeHandlers = getTypeHandlers(typeHandlerRegistry, metaParam, keyProperties, rsmd);
                }
                populateKeys(rs, metaParam, keyProperties, typeHandlers);
            }
        }
    } catch (Exception e) {
        throw new ExecutorException("Error getting generated key or setting result to parameter object. Cause: " + e, e);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
            // ignore
            }
        }
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) ExecutorException(org.apache.ibatis.executor.ExecutorException) Configuration(org.apache.ibatis.session.Configuration) MetaObject(org.apache.ibatis.reflection.MetaObject) ResultSet(java.sql.ResultSet) MetaObject(org.apache.ibatis.reflection.MetaObject) TypeHandler(org.apache.ibatis.type.TypeHandler) ExecutorException(org.apache.ibatis.executor.ExecutorException) SQLException(java.sql.SQLException)

Example 3 with ExecutorException

use of org.apache.ibatis.executor.ExecutorException 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 4 with ExecutorException

use of org.apache.ibatis.executor.ExecutorException 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)

Example 5 with ExecutorException

use of org.apache.ibatis.executor.ExecutorException in project mybatis-3 by mybatis.

the class DefaultResultSetHandler method createParameterizedResultObject.

Object createParameterizedResultObject(ResultSetWrapper rsw, Class<?> resultType, List<ResultMapping> constructorMappings, List<Class<?>> constructorArgTypes, List<Object> constructorArgs, String columnPrefix) {
    boolean foundValues = false;
    for (ResultMapping constructorMapping : constructorMappings) {
        final Class<?> parameterType = constructorMapping.getJavaType();
        final String column = constructorMapping.getColumn();
        final Object value;
        try {
            if (constructorMapping.getNestedQueryId() != null) {
                value = getNestedQueryConstructorValue(rsw.getResultSet(), constructorMapping, columnPrefix);
            } else if (constructorMapping.getNestedResultMapId() != null) {
                final ResultMap resultMap = configuration.getResultMap(constructorMapping.getNestedResultMapId());
                value = getRowValue(rsw, resultMap);
            } else {
                final TypeHandler<?> typeHandler = constructorMapping.getTypeHandler();
                value = typeHandler.getResult(rsw.getResultSet(), prependPrefix(column, columnPrefix));
            }
        } catch (ResultMapException e) {
            throw new ExecutorException("Could not process result for mapping: " + constructorMapping, e);
        } catch (SQLException e) {
            throw new ExecutorException("Could not process result for mapping: " + constructorMapping, e);
        }
        constructorArgTypes.add(parameterType);
        constructorArgs.add(value);
        foundValues = value != null || foundValues;
    }
    return foundValues ? objectFactory.create(resultType, constructorArgTypes, constructorArgs) : null;
}
Also used : ResultMap(org.apache.ibatis.mapping.ResultMap) ExecutorException(org.apache.ibatis.executor.ExecutorException) SQLException(java.sql.SQLException) ResultMapping(org.apache.ibatis.mapping.ResultMapping) ResultMapException(org.apache.ibatis.executor.result.ResultMapException) MetaObject(org.apache.ibatis.reflection.MetaObject) TypeHandler(org.apache.ibatis.type.TypeHandler)

Aggregations

ExecutorException (org.apache.ibatis.executor.ExecutorException)15 MetaObject (org.apache.ibatis.reflection.MetaObject)7 SQLException (java.sql.SQLException)6 ResultMapping (org.apache.ibatis.mapping.ResultMapping)5 TypeHandler (org.apache.ibatis.type.TypeHandler)4 CacheKey (org.apache.ibatis.cache.CacheKey)3 ResultMap (org.apache.ibatis.mapping.ResultMap)3 ResultSet (java.sql.ResultSet)2 ResultMapException (org.apache.ibatis.executor.result.ResultMapException)2 MappedStatement (org.apache.ibatis.mapping.MappedStatement)2 Configuration (org.apache.ibatis.session.Configuration)2 ResultSetMetaData (java.sql.ResultSetMetaData)1 Statement (java.sql.Statement)1 Proxy (javassist.util.proxy.Proxy)1 ProxyFactory (javassist.util.proxy.ProxyFactory)1 DataSource (javax.sql.DataSource)1 DefaultCursor (org.apache.ibatis.cursor.defaults.DefaultCursor)1 Executor (org.apache.ibatis.executor.Executor)1 AbstractEnhancedDeserializationProxy (org.apache.ibatis.executor.loader.AbstractEnhancedDeserializationProxy)1 WriteReplaceInterface (org.apache.ibatis.executor.loader.WriteReplaceInterface)1