{"record":{"id":"7067930009ec70a0","repo":"mybatis/mybatis-3","slug":"cannot-define-both-nestedqueryid-and-nestedresultm","errorCode":null,"errorMessage":"Cannot define both nestedQueryId and nestedResultMapId in property {}","messagePattern":"Cannot define both nestedQueryId and nestedResultMapId in property (.+?)","errorType":"validation","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/mapping/ResultMapping.java","lineNumber":163,"sourceCode":"    }\n\n    public Builder lazy(boolean lazy) {\n      resultMapping.lazy = lazy;\n      return this;\n    }\n\n    public ResultMapping build() {\n      // lock down collections\n      resultMapping.flags = List.copyOf(resultMapping.flags);\n      resultMapping.composites = List.copyOf(resultMapping.composites);\n      validate();\n      return resultMapping;\n    }\n\n    private void validate() {\n      // Issue #697: cannot define both nestedQueryId and nestedResultMapId\n      if (resultMapping.nestedQueryId != null && resultMapping.nestedResultMapId != null) {\n        throw new IllegalStateException(\n            \"Cannot define both nestedQueryId and nestedResultMapId in property \" + resultMapping.property);\n      }\n      // Issue #4 and GH #39: column is optional only in nested resultMaps but not in the rest\n      if (resultMapping.nestedResultMapId == null && resultMapping.column == null\n          && resultMapping.composites.isEmpty()) {\n        throw new IllegalStateException(\"Mapping is missing column attribute for property \" + resultMapping.property);\n      }\n      if (resultMapping.getResultSet() != null) {\n        int numColumns = 0;\n        if (resultMapping.column != null) {\n          numColumns = resultMapping.column.split(\",\").length;\n        }\n        int numForeignColumns = 0;\n        if (resultMapping.foreignColumn != null) {\n          numForeignColumns = resultMapping.foreignColumn.split(\",\").length;\n        }\n        if (numColumns != numForeignColumns) {\n          throw new IllegalStateException(","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/mapping/ResultMapping.java#L145-L181","documentation":"ResultMapping.Builder.validate() throws IllegalStateException when a single property mapping declares both a nested select (select=) and a nested resultMap (resultMap=). MyBatis cannot load a collection/association both by executing another statement and by nested result-map mapping at the same time (Issue #697), so the combination is rejected at build time.","triggerScenarios":"XML like <collection property=\"items\" column=\"order_id\" select=\"loadItems\" resultMap=\"itemResultMap\"/> — one mapping element carrying both attributes; same for <association>. Programmatic equivalent: ResultMapping.Builder with both nestedQueryId and nestedResultMapId set.","commonSituations":"Refactoring a lazy-loaded collection (select=) to a joined-fetch resultMap and forgetting to delete the select attribute; copy-paste between a nested-select mapping and a join mapping; merging mapper fragments during cleanup.","solutions":["Choose one strategy per mapping: keep select= for lazy/nested-select loading, or keep resultMap= for join-based nested mapping, and delete the other attribute.","If you need both lazy and joined behavior, define two separate resultMaps (one per strategy) and switch at the statement level.","For join-based collections, remember column= is still required and the nested resultMap must map the child columns."],"exampleFix":"<!-- before -->\n<resultMap id=\"orderMap\" type=\"Order\">\n  <collection property=\"items\" column=\"order_id\"\n              select=\"loadItems\" resultMap=\"itemMap\"/>\n</resultMap>\n\n<!-- after (nested resultMap / join fetch) -->\n<resultMap id=\"orderMap\" type=\"Order\">\n  <collection property=\"items\" resultMap=\"itemMap\"/>\n</resultMap>\n<!-- or, for lazy loading: <collection property=\"items\" column=\"order_id\" select=\"loadItems\"/> -->","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Rule of thumb: select= OR resultMap= on a collection/association, never both.","When refactoring from lazy select= to join-based resultMap=, delete the select attribute in the same commit.","Cover both mapping styles with integration tests so attribute conflicts fail in CI, not at runtime."],"tags":["mybatis","result-map","nested-mapping","configuration"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}