{"record":{"id":"b8d484f1bc74381f","repo":"baomidou/mybatis-plus","slug":"ambiguous-collection-type-for-property-s-you-m","errorCode":null,"errorMessage":"Ambiguous collection type for property '%s'. You must specify 'javaType' or 'resultMap'.","messagePattern":"Ambiguous collection type for property '(.+?)'\\. You must specify 'javaType' or 'resultMap'\\.","errorType":"exception","errorClass":"BuilderException","httpStatus":null,"severity":"error","filePath":"mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisXMLMapperBuilder.java","lineNumber":381,"sourceCode":"\n    private String processNestedResultMappings(XNode context, List<ResultMapping> resultMappings,\n                                               Class<?> enclosingType) {\n        if (Arrays.asList(\"association\", \"collection\", \"case\").contains(context.getName())\n            && context.getStringAttribute(\"select\") == null) {\n            validateCollection(context, enclosingType);\n            ResultMap resultMap = resultMapElement(context, resultMappings, enclosingType);\n            return resultMap.getId();\n        }\n        return null;\n    }\n\n    protected void validateCollection(XNode context, Class<?> enclosingType) {\n        if (\"collection\".equals(context.getName()) && context.getStringAttribute(\"resultMap\") == null\n            && context.getStringAttribute(\"javaType\") == null) {\n            MetaClass metaResultType = MetaClass.forClass(enclosingType, configuration.getReflectorFactory());\n            String property = context.getStringAttribute(\"property\");\n            if (!metaResultType.hasSetter(property)) {\n                throw new BuilderException(\n                    \"Ambiguous collection type for property '\" + property + \"'. You must specify 'javaType' or 'resultMap'.\");\n            }\n        }\n    }\n\n    private void bindMapperForNamespace() {\n        String namespace = builderAssistant.getCurrentNamespace();\n        if (namespace != null) {\n            Class<?> boundType = null;\n            try {\n                boundType = Resources.classForName(namespace);\n            } catch (ClassNotFoundException e) {\n                // ignore, bound type is not required\n            }\n            if (boundType != null && !configuration.hasMapper(boundType)) {\n                // Spring may not know the real resource name so we set a flag\n                // to prevent loading again this resource from the mapper interface\n                // look at MapperAnnotationBuilder#loadXmlResource","sourceCodeStart":363,"sourceCodeEnd":399,"githubUrl":"https://github.com/baomidou/mybatis-plus/blob/bf67d907478c724120bf76292da54abf9e73c2b3/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisXMLMapperBuilder.java#L363-L399","documentation":"A <collection> element in a resultMap declares neither 'resultMap' nor 'javaType', and the enclosing result type has no setter for the named property, so MyBatis cannot infer the collection's element/target type. MyBatis-Plus adds this validation to surface the ambiguity at parse time instead of failing obscurely at mapping time.","triggerScenarios":"resultMap with <collection property=\"items\"> where the enclosing class lacks a setItems(...) setter (or the property name is misspelled), and no javaType or resultMap attribute is provided on the collection.","commonSituations":"Renaming the Java property but not the XML; using a field with no setter (immutable DTO or Lombok @Value); Lombok not processed so setters are absent; nested collections where the wrong 'ofType'/'resultMap' was omitted.","solutions":["Add an explicit javaType (e.g. javaType=\"java.util.List\") and ofType to the <collection>","Better: reference a nested resultMap via resultMap=\"itemResultMap\" on the <collection>","Otherwise fix the property name or add a setter for it on the enclosing type","If using Lombok, confirm annotation processing is enabled so setters are generated"],"exampleFix":"<!-- before -->\n<collection property=\"orderItems\" ofType=\"com.example.Item\"/>\n<!-- after -->\n<collection property=\"orderItems\" javaType=\"java.util.ArrayList\" ofType=\"com.example.Item\"/>\n<!-- or -->\n<collection property=\"orderItems\" resultMap=\"itemResultMap\"/>","handlingStrategy":"validation","validationCode":"// Unit-test your resultMaps against the enclosing type before shipping:\nMetaClass mc = MetaClass.forClass(Order.class, new DefaultReflectorFactory());\nif (!mc.hasSetter(\"orderItems\")) throw new IllegalStateException(\"resultMap property 'orderItems' has no setter\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always specify resultMap or javaType+ofType on <collection> elements","Mirror every resultMap with a mapping unit test (call sqlSession select on a sample row in an H2 test)","If using Lombok, keep @Data (with setters) on resultMap target DTOs"],"tags":["mybatis","resultmap","xml","mapping"],"backgroundTag":null,"analyzedSha":"bf67d907478c724120bf76292da54abf9e73c2b3","analyzedAt":"2026-08-14T15:17:09.543Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}