{"record":{"id":"51dc1eaf8209d3cd","repo":"mybatis/mybatis-3","slug":"too-many-keys-are-generated-there-are-only-d-tar-51dc1e","errorCode":null,"errorMessage":"Too many keys are generated. There are only %d target objects. You either specified a wrong 'keyProperty' or encountered a driver bug like #1523.","messagePattern":"Too many keys are generated\\. There are only (.+?) target objects\\. You either specified a wrong 'keyProperty' or encountered a driver bug like #1523\\.","errorType":"exception","errorClass":"ExecutorException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java","lineNumber":141,"sourceCode":"    Iterator<?> iterator = params.iterator();\n    while (rs.next()) {\n      if (!iterator.hasNext()) {\n        throw new ExecutorException(\n            String.format(parameter instanceof Map ? MSG_TOO_MANY_KEYS_FOR_MAP : MSG_TOO_MANY_KEYS, params.size()));\n      }\n      Object param = iterator.next();\n      assignerList.forEach(x -> x.assign(rs, param));\n    }\n  }\n\n  private void assignKeysToParamMapList(Configuration configuration, ResultSet rs, ResultSetMetaData rsmd,\n      String[] keyProperties, ArrayList<ParamMap<?>> paramMapList) throws SQLException {\n    Iterator<ParamMap<?>> iterator = paramMapList.iterator();\n    List<KeyAssigner> assignerList = new ArrayList<>();\n    long counter = 0;\n    while (rs.next()) {\n      if (!iterator.hasNext()) {\n        throw new ExecutorException(String.format(MSG_TOO_MANY_KEYS, counter));\n      }\n      ParamMap<?> paramMap = iterator.next();\n      if (assignerList.isEmpty()) {\n        for (int i = 0; i < keyProperties.length; i++) {\n          assignerList\n              .add(getAssignerForParamMap(configuration, rsmd, i + 1, paramMap, keyProperties[i], keyProperties, false)\n                  .getValue());\n        }\n      }\n      assignerList.forEach(x -> x.assign(rs, paramMap));\n      counter++;\n    }\n  }\n\n  private void assignKeysToParamMap(Configuration configuration, ResultSet rs, ResultSetMetaData rsmd,\n      String[] keyProperties, Map<String, ?> paramMap) throws SQLException {\n    if (paramMap.isEmpty()) {\n      return;","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java#L123-L159","documentation":"assignKeysToParamMapList handles batch inserts where the parameter is a list of ParamMap (multi-param or @Param methods). It walks the generated-keys ResultSet and the parameter list in lockstep; if the driver yields more key rows than there are parameter objects, the counts cannot be reconciled. The message points at a wrong keyProperty or a known driver bug (#1523) where drivers return bogus extra generated-key rows.","triggerScenarios":"Batch insert (ExecutorType.BATCH, or multi-row VALUES) with useGeneratedKeys and @Param parameters, using a driver that reports more generated-key rows than inserted rows (bug referenced as issue #1523), or a keyProperty that makes MyBatis misalign the mapping.","commonSituations":"Upgrading a JDBC driver that changes getGeneratedKeys() behavior for batches; SQL Server / MySQL driver versions returning keys for every batch execution segment.","solutions":["Upgrade (or roll back) the JDBC driver to a version where getGeneratedKeys() returns exactly one row per inserted row","Verify keyProperty matches a real writable property on each parameter object","For batch inserts, set keyProperty with the parameter prefix (e.g. 'items.id') as documented for @Param usage","If the driver bug cannot be avoided, execute inserts one row per statement or use <selectKey> per row"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { mapper.batchInsert(users); } catch (PersistenceException e) { if (String.valueOf(e.getMessage()).contains(\"Too many keys\")) { log.error(\"driver key count mismatch — check keyProperty / upgrade JDBC driver (issue #1523)\"); } throw e; }","preventionTips":["Pin the JDBC driver version and regression-test batch inserts after upgrades","Verify keyProperty names against the parameter classes before shipping"],"tags":["generated-keys","batch-insert","driver-bug"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}