mybatis/mybatis-3 · error · ExecutorException

Error selecting key or setting result to parameter object. C

Error message

Error selecting key or setting result to parameter object. Cause: ${cause}

What it means

processGeneratedKeys wraps the entire selectKey execution — running the key query, reading its value, and setting it on the parameter object — in a try/catch. ExecutorExceptions pass through untouched; any other exception (SQL failure, reflection error, type conversion) is rethrown as this generic ExecutorException with the cause appended.

Source

Thrown at src/main/java/org/apache/ibatis/executor/keygen/SelectKeyGenerator.java:91

        } else {
          MetaObject metaResult = configuration.newMetaObject(values.get(0));
          if (keyProperties.length == 1) {
            if (metaResult.hasGetter(keyProperties[0])) {
              setValue(metaParam, keyProperties[0], metaResult.getValue(keyProperties[0]));
            } else {
              // no getter for the property - maybe just a single value object
              // so try that
              setValue(metaParam, keyProperties[0], values.get(0));
            }
          } else {
            handleMultipleProperties(keyProperties, metaParam, metaResult);
          }
        }
      }
    } catch (ExecutorException e) {
      throw e;
    } catch (Exception e) {
      throw new ExecutorException("Error selecting key or setting result to parameter object. Cause: " + e, e);
    }
  }

  private void handleMultipleProperties(String[] keyProperties, MetaObject metaParam, MetaObject metaResult) {
    String[] keyColumns = keyStatement.getKeyColumns();

    if (keyColumns == null || keyColumns.length == 0) {
      // no key columns specified, just use the property names
      for (String keyProperty : keyProperties) {
        setValue(metaParam, keyProperty, metaResult.getValue(keyProperty));
      }
    } else {
      if (keyColumns.length != keyProperties.length) {
        throw new ExecutorException(
            "If SelectKey has key columns, the number must match the number of key properties.");
      }
      for (int i = 0; i < keyProperties.length; i++) {
        setValue(metaParam, keyProperties[i], metaResult.getValue(keyColumns[i]));

View on GitHub (pinned to 008069adb1)

Solutions

  1. Read the 'Cause:' — it carries the real SQLException/RuntimeException
  2. Fix the underlying SQL/schema/permission problem it identifies
  3. Align selectKey resultType with the keyProperty field type
Defensive patterns

Strategy: try-catch

Try / catch

try { mapper.insert(user); } catch (PersistenceException e) { Throwable c = e.getCause(); if (c instanceof ExecutorException && String.valueOf(c.getMessage()).startsWith("Error selecting key")) { log.error("selectKey execution failed, root cause:", c.getCause()); } throw e; }

Prevention

When it happens

Trigger: The <selectKey> SQL itself failing (bad SQL, missing table/sequence, permissions, connection issues during key execution), or setValue failing with a non-Executor exception such as a type mismatch when writing the key to the parameter.

Common situations: DB permissions for the sequence missing, wrong DB schema in an environment, connection pool exhaustion at insert time, keyProperty type incompatible with selectKey resultType.

Related errors


AI-assisted analysis of mybatis/mybatis-3@008069adb1 (2026-08-14). Data as JSON: /api/errors/92f5d0d80bbfc517. Report an issue: GitHub.