{"record":{"id":"4348c2a6f8978ddf","repo":"mybatis/mybatis-3","slug":"error-cloning-cache-key-cause","errorCode":null,"errorMessage":"Error cloning cache key.  Cause: {}","messagePattern":"Error cloning cache key\\.  Cause: (.+?)","errorType":"exception","errorClass":"ExecutorException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java","lineNumber":1519,"sourceCode":"      } else {\n        createRowKeyForUnmappedProperties(resultMap, rsw, cacheKey, columnPrefix);\n      }\n    } else {\n      createRowKeyForMappedProperties(resultMap, rsw, cacheKey, resultMappings, columnPrefix);\n    }\n    if (cacheKey.getUpdateCount() < 2) {\n      return CacheKey.NULL_CACHE_KEY;\n    }\n    return cacheKey;\n  }\n\n  private CacheKey combineKeys(CacheKey rowKey, CacheKey parentRowKey) {\n    if (rowKey.getUpdateCount() > 1 && parentRowKey.getUpdateCount() > 1) {\n      CacheKey combinedKey;\n      try {\n        combinedKey = rowKey.clone();\n      } catch (CloneNotSupportedException e) {\n        throw new ExecutorException(\"Error cloning cache key.  Cause: \" + e, e);\n      }\n      combinedKey.update(parentRowKey);\n      return combinedKey;\n    }\n    return CacheKey.NULL_CACHE_KEY;\n  }\n\n  private List<ResultMapping> getResultMappingsForRowKey(ResultMap resultMap) {\n    List<ResultMapping> resultMappings = resultMap.getIdResultMappings();\n    if (resultMappings.isEmpty()) {\n      resultMappings = resultMap.getPropertyResultMappings();\n    }\n    return resultMappings;\n  }\n\n  private void createRowKeyForMappedProperties(ResultMap resultMap, ResultSetWrapper rsw, CacheKey cacheKey,\n      List<ResultMapping> resultMappings, String columnPrefix) throws SQLException {\n    for (ResultMapping resultMapping : resultMappings) {","sourceCodeStart":1501,"sourceCodeEnd":1537,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java#L1501-L1537","documentation":"While combining the row cache key of a nested result with its parent row key (used to deduplicate nested rows for collections/associations), cloning the CacheKey threw CloneNotSupportedException. CacheKey implements clone() and this is a defensive wrap that in practice should never fire; if it does, it indicates a corrupted/patched mybatis build or an unexpected runtime environment rather than a user mapping error.","triggerScenarios":"Nested result mapping with multiple id/contributing columns on both parent and nested rows (both rowKey and parentRowKey update counts > 1) so combineKeys performs rowKey.clone(); clone() throws CloneNotSupportedException.","commonSituations":"Essentially untriggerable with stock MyBatis (CacheKey.clone() never throws); appears only with modified classpath versions, old forked copies of mybatis on the classpath shadowing the real one, or exotic bytecode transformations.","solutions":["Verify only one MyBatis jar is on the classpath and it is the official unmodified artifact (mvn dependency:tree | grep mybatis).","Clean and rebuild to eliminate stale instrumented classes.","If the exception occurs, report a bug to the MyBatis project with the full stack trace — stock CacheKey cannot throw this."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// verify a clean classpath at startup\nnew org.apache.ibatis.cache.CacheKey().clone(); // must never throw; smoke-test the mybatis jar","typeGuard":null,"tryCatchPattern":"catch (ExecutorException e) when e.getMessage().contains(\"Error cloning cache key\") -> treat as environment/classpath defect: fail startup and require dependency audit rather than continuing with possibly-corrupted mapping state.","preventionTips":["Pin a single official mybatis version via dependency management; ban duplicate artifacts with an enforcer rule.","Never mix forks and official jars in one deployment."],"tags":["mybatis","cachekey","internal","defensive"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}