{"record":{"id":"92f5d0d80bbfc517","repo":"mybatis/mybatis-3","slug":"error-selecting-key-or-setting-result-to-parameter","errorCode":null,"errorMessage":"Error selecting key or setting result to parameter object. Cause: ${cause}","messagePattern":"Error selecting key or setting result to parameter object\\. Cause: (.+?)","errorType":"exception","errorClass":"ExecutorException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/executor/keygen/SelectKeyGenerator.java","lineNumber":91,"sourceCode":"        } else {\n          MetaObject metaResult = configuration.newMetaObject(values.get(0));\n          if (keyProperties.length == 1) {\n            if (metaResult.hasGetter(keyProperties[0])) {\n              setValue(metaParam, keyProperties[0], metaResult.getValue(keyProperties[0]));\n            } else {\n              // no getter for the property - maybe just a single value object\n              // so try that\n              setValue(metaParam, keyProperties[0], values.get(0));\n            }\n          } else {\n            handleMultipleProperties(keyProperties, metaParam, metaResult);\n          }\n        }\n      }\n    } catch (ExecutorException e) {\n      throw e;\n    } catch (Exception e) {\n      throw new ExecutorException(\"Error selecting key or setting result to parameter object. Cause: \" + e, e);\n    }\n  }\n\n  private void handleMultipleProperties(String[] keyProperties, MetaObject metaParam, MetaObject metaResult) {\n    String[] keyColumns = keyStatement.getKeyColumns();\n\n    if (keyColumns == null || keyColumns.length == 0) {\n      // no key columns specified, just use the property names\n      for (String keyProperty : keyProperties) {\n        setValue(metaParam, keyProperty, metaResult.getValue(keyProperty));\n      }\n    } else {\n      if (keyColumns.length != keyProperties.length) {\n        throw new ExecutorException(\n            \"If SelectKey has key columns, the number must match the number of key properties.\");\n      }\n      for (int i = 0; i < keyProperties.length; i++) {\n        setValue(metaParam, keyProperties[i], metaResult.getValue(keyColumns[i]));","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/executor/keygen/SelectKeyGenerator.java#L73-L109","documentation":"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.","triggerScenarios":"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.","commonSituations":"DB permissions for the sequence missing, wrong DB schema in an environment, connection pool exhaustion at insert time, keyProperty type incompatible with selectKey resultType.","solutions":["Read the 'Cause:' — it carries the real SQLException/RuntimeException","Fix the underlying SQL/schema/permission problem it identifies","Align selectKey resultType with the keyProperty field type"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"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; }","preventionTips":["Grant sequence/table permissions needed by selectKey statements in every environment","Log and inspect the wrapped cause, never just the outer message"],"tags":["selectkey","wrapper","jdbc"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}