mybatis/mybatis-3 · error · ExecutorException
Error getting generated key or setting result to parameter o
Error message
Error getting generated key or setting result to parameter object. Cause: ${cause} What it means
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.
Source
Thrown at src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java:93
public void processAfter(Executor executor, MappedStatement ms, Statement stmt, Object parameter) {
processBatch(ms, stmt, parameter);
}
public void processBatch(MappedStatement ms, Statement stmt, Object parameter) {
final String[] keyProperties = ms.getKeyProperties();
if (keyProperties == null || keyProperties.length == 0) {
return;
}
try (ResultSet rs = stmt.getGeneratedKeys()) {
final ResultSetMetaData rsmd = rs.getMetaData();
final Configuration configuration = ms.getConfiguration();
if (rsmd.getColumnCount() < keyProperties.length) {
// Error?
} else {
assignKeys(configuration, rs, rsmd, keyProperties, parameter);
}
} catch (Exception e) {
throw new ExecutorException("Error getting generated key or setting result to parameter object. Cause: " + e, e);
}
}
@SuppressWarnings("unchecked")
private void assignKeys(Configuration configuration, ResultSet rs, ResultSetMetaData rsmd, String[] keyProperties,
Object parameter) throws SQLException {
if (parameter instanceof ParamMap || parameter instanceof StrictMap) {
// Multi-param or single param with @Param
assignKeysToParamMap(configuration, rs, rsmd, keyProperties, (Map<String, ?>) parameter);
} else if (parameter instanceof ArrayList && !((ArrayList<?>) parameter).isEmpty()
&& ((ArrayList<?>) parameter).get(0) instanceof ParamMap) {
// Multi-param or single param with @Param in batch operation
assignKeysToParamMapList(configuration, rs, rsmd, keyProperties, (ArrayList<ParamMap<?>>) parameter);
} else {
// Single param without @Param
assignKeysToParam(configuration, rs, rsmd, keyProperties, parameter);
}
}View on GitHub (pinned to 008069adb1)
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
Example fix
<!-- before --> <insert id="insert" useGeneratedKeys="true" keyProperty="id"> <!-- after: pin the column when driver returns extra columns --> <insert id="insert" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
Defensive patterns
Strategy: try-catch
Try / catch
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; } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Error accessing PooledConnection. Connection is invalid.
- Too many keys are generated. There are only %d target object
- Too many keys are generated. There are only %d target object
- Could not determine which parameter to assign generated keys
- Could not find parameter '${paramName}'. Note that when ther
AI-assisted analysis of mybatis/mybatis-3@008069adb1 (2026-08-14).
Data as JSON: /api/errors/1dd9ac3923537937.
Report an issue: GitHub.