{"record":{"id":"1dd9ac3923537937","repo":"mybatis/mybatis-3","slug":"error-getting-generated-key-or-setting-result-to-p","errorCode":null,"errorMessage":"Error getting generated key or setting result to parameter object. Cause: ${cause}","messagePattern":"Error getting generated 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/Jdbc3KeyGenerator.java","lineNumber":93,"sourceCode":"  public void processAfter(Executor executor, MappedStatement ms, Statement stmt, Object parameter) {\n    processBatch(ms, stmt, parameter);\n  }\n\n  public void processBatch(MappedStatement ms, Statement stmt, Object parameter) {\n    final String[] keyProperties = ms.getKeyProperties();\n    if (keyProperties == null || keyProperties.length == 0) {\n      return;\n    }\n    try (ResultSet rs = stmt.getGeneratedKeys()) {\n      final ResultSetMetaData rsmd = rs.getMetaData();\n      final Configuration configuration = ms.getConfiguration();\n      if (rsmd.getColumnCount() < keyProperties.length) {\n        // Error?\n      } else {\n        assignKeys(configuration, rs, rsmd, keyProperties, parameter);\n      }\n    } catch (Exception e) {\n      throw new ExecutorException(\"Error getting generated key or setting result to parameter object. Cause: \" + e, e);\n    }\n  }\n\n  @SuppressWarnings(\"unchecked\")\n  private void assignKeys(Configuration configuration, ResultSet rs, ResultSetMetaData rsmd, String[] keyProperties,\n      Object parameter) throws SQLException {\n    if (parameter instanceof ParamMap || parameter instanceof StrictMap) {\n      // Multi-param or single param with @Param\n      assignKeysToParamMap(configuration, rs, rsmd, keyProperties, (Map<String, ?>) parameter);\n    } else if (parameter instanceof ArrayList && !((ArrayList<?>) parameter).isEmpty()\n        && ((ArrayList<?>) parameter).get(0) instanceof ParamMap) {\n      // Multi-param or single param with @Param in batch operation\n      assignKeysToParamMapList(configuration, rs, rsmd, keyProperties, (ArrayList<ParamMap<?>>) parameter);\n    } else {\n      // Single param without @Param\n      assignKeysToParam(configuration, rs, rsmd, keyProperties, parameter);\n    }\n  }","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java#L75-L111","documentation":"Jdbc3KeyGenerator.processBatch reads auto-generated keys from the JDBC Statement via getGeneratedKeys() and assigns them to the parameter object's keyProperty. Any exception during that read or assignment (driver failing on getGeneratedKeys, metadata problems, type handler failures, reflection errors) is wrapped in this ExecutorException with the cause appended.","triggerScenarios":"INSERT with useGeneratedKeys=\"true\" on a driver that does not support Statement.getGeneratedKeys(); the generated key column type having no registered TypeHandler for the keyProperty type; the keyProperty not existing on the parameter object (assignment reflection failure); a driver returning keys in an unexpected format.","commonSituations":"Oracle with old JDBC drivers, keys typed as BigDecimal being assigned to a Long property, batch inserts with drivers that return generated keys in odd shapes, and mismatches between keyColumn and the actual generated column.","solutions":["Read the appended 'Cause:' — the wrapped exception identifies the real problem","Declare keyColumn=\"ID\" (or the actual generated column) so the right column is read","Make the keyProperty type compatible with the generated key type, or register a TypeHandler for it","If the driver does not support getGeneratedKeys, switch to a <selectKey> statement instead"],"exampleFix":"<!-- before -->\n<insert id=\"insert\" useGeneratedKeys=\"true\" keyProperty=\"id\">\n\n<!-- after: pin the column when driver returns extra columns -->\n<insert id=\"insert\" useGeneratedKeys=\"true\" keyProperty=\"id\" keyColumn=\"id\">","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { mapper.insert(user); } catch (PersistenceException e) { Throwable cause = e.getCause(); if (cause instanceof ExecutorException && String.valueOf(cause.getMessage()).startsWith(\"Error getting generated key\")) { log.error(\"generated key failure, cause:\", cause.getCause()); } throw e; }","preventionTips":["Always declare keyColumn alongside useGeneratedKeys for portable drivers","Match keyProperty type to the generated column type","Test inserts against the actual target database driver, not just H2"],"tags":["generated-keys","insert","jdbc"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}