{"record":{"id":"8b0c5bbee1a515b4","repo":"mybatis/mybatis-3","slug":"cannot-use-both-one-and-many-annotations-in-the","errorCode":null,"errorMessage":"Cannot use both @One and @Many annotations in the same @Result","messagePattern":"Cannot use both @One and @Many annotations in the same @Result","errorType":"exception","errorClass":"BuilderException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java","lineNumber":542,"sourceCode":"      columnPrefix = result.many().columnPrefix();\n    }\n    return columnPrefix;\n  }\n\n  private String nestedResultMapId(Result result) {\n    String resultMapId = result.one().resultMap();\n    if (resultMapId.isEmpty()) {\n      resultMapId = result.many().resultMap();\n    }\n    if (!resultMapId.contains(\".\")) {\n      resultMapId = type.getName() + \".\" + resultMapId;\n    }\n    return resultMapId;\n  }\n\n  private boolean hasNestedResultMap(Result result) {\n    if (!result.one().resultMap().isEmpty() && !result.many().resultMap().isEmpty()) {\n      throw new BuilderException(\"Cannot use both @One and @Many annotations in the same @Result\");\n    }\n    return !result.one().resultMap().isEmpty() || !result.many().resultMap().isEmpty();\n  }\n\n  private String nestedSelectId(Result result) {\n    String nestedSelect = result.one().select();\n    if (nestedSelect.isEmpty()) {\n      nestedSelect = result.many().select();\n    }\n    if (!nestedSelect.contains(\".\")) {\n      nestedSelect = type.getName() + \".\" + nestedSelect;\n    }\n    return nestedSelect;\n  }\n\n  private boolean isLazy(Result result) {\n    boolean isLazy = configuration.isLazyLoadingEnabled();\n    if (!result.one().select().isEmpty() && FetchType.DEFAULT != result.one().fetchType()) {","sourceCodeStart":524,"sourceCodeEnd":560,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java#L524-L560","documentation":"Thrown by MapperAnnotationBuilder.hasNestedResultMap(): while inspecting an @Result for a nested result map, both @Result(one = @One(resultMap=...)) and @Result(many = @Many(resultMap=...)) are populated. A single association property cannot be both a single-object (@One) and a collection (@Many) mapping, so the ambiguity is rejected.","triggerScenarios":"@Result(property = \"items\", one = @One(resultMap = \"itemMap\"), many = @Many(resultMap = \"itemMap\")) — both annotation attributes non-default on the same @Result.","commonSituations":"Refactoring a one-to-one mapping into a collection (or vice versa) and leaving the old annotation in place; IDE auto-complete generating both stubs; copy-paste of a @Result that already had one side filled.","solutions":["Keep exactly one: @One for single-object associations (nested resultMap or select) or @Many for collections","For collection properties use @Many; for to-one relations use @One","Remove the unused annotation attribute entirely"],"exampleFix":"// before\n@Result(property = \"orders\", column = \"id\", one = @One(resultMap = \"orderMap\"), many = @Many(resultMap = \"orderMap\"))\n// after\n@Result(property = \"orders\", column = \"id\", many = @Many(resultMap = \"orderMap\"))","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static void checkResult(Result r) {\n  boolean hasOne = !r.one().resultMap().isEmpty() || !r.one().select().isEmpty();\n  boolean hasMany = !r.many().resultMap().isEmpty() || !r.many().select().isEmpty();\n  if (hasOne && hasMany) {\n    throw new IllegalStateException(\"@Result(property=\" + r.property() + \") declares both @One and @Many\");\n  }\n}","tryCatchPattern":null,"preventionTips":["Match multiplicity: single object → @One, collection → @Many, never both","Delete the unused half when changing an association's kind"],"tags":["mybatis","annotation","nested-mapping","resultmap"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}