{"record":{"id":"f896e8786a95fdbf","repo":"baomidou/mybatis-plus","slug":"too-many-default-otherwise-elements-in-choose-st","errorCode":null,"errorMessage":"Too many default (otherwise) elements in choose statement.","messagePattern":"Too many default \\(otherwise\\) elements in choose statement\\.","errorType":"exception","errorClass":"BuilderException","httpStatus":null,"severity":"error","filePath":"mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisXMLScriptBuilder.java","lineNumber":320,"sourceCode":"        private void handleWhenOtherwiseNodes(XNode chooseSqlNode, List<SqlNode> ifSqlNodes, List<SqlNode> defaultSqlNodes) {\n            List<XNode> children = chooseSqlNode.getChildren();\n            for (XNode child : children) {\n                String nodeName = child.getNode().getNodeName();\n                NodeHandler handler = nodeHandlerMap.get(nodeName);\n                if (handler instanceof IfHandler) {\n                    handler.handleNode(child, ifSqlNodes);\n                } else if (handler instanceof OtherwiseHandler) {\n                    handler.handleNode(child, defaultSqlNodes);\n                }\n            }\n        }\n\n        private SqlNode getDefaultSqlNode(List<SqlNode> defaultSqlNodes) {\n            SqlNode defaultSqlNode = null;\n            if (defaultSqlNodes.size() == 1) {\n                defaultSqlNode = defaultSqlNodes.get(0);\n            } else if (defaultSqlNodes.size() > 1) {\n                throw new BuilderException(\"Too many default (otherwise) elements in choose statement.\");\n            }\n            return defaultSqlNode;\n        }\n    }\n\n}\n","sourceCodeStart":302,"sourceCodeEnd":327,"githubUrl":"https://github.com/baomidou/mybatis-plus/blob/bf67d907478c724120bf76292da54abf9e73c2b3/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisXMLScriptBuilder.java#L302-L327","documentation":"Within a single <choose> element, more than one <otherwise> branch was found. The choose/when/otherwise construct models if/else-if/else, so at most one default (otherwise) branch is allowed; the builder enforces this while assembling the default SqlNode.","triggerScenarios":"A <choose> block containing two or more <otherwise> elements, usually from copy-pasting a <when> block and forgetting to change the tag, or merged XML from a bad conflict resolution.","commonSituations":"Git merge conflicts resolved by keeping both branches; copy-paste of conditional blocks; hand-editing long dynamic SQL.","solutions":["Locate the choose block in the mapper named by the parse error and delete all but one <otherwise>","If multiple fallback conditions are truly needed, convert the extra <otherwise> blocks into <when test=\"...\"> branches with explicit conditions","Refactor long choose blocks into <sql>/<include> fragments to reduce merge-conflict risk"],"exampleFix":"<!-- before -->\n<choose>\n  <when test=\"a\">A</when>\n  <otherwise>B</otherwise>\n  <otherwise>C</otherwise>\n</choose>\n<!-- after -->\n<choose>\n  <when test=\"a\">A</when>\n  <when test=\"b\">B</when>\n  <otherwise>C</otherwise>\n</choose>","handlingStrategy":"validation","validationCode":"// Check choose blocks during build: at most one otherwise\nElement choose = (Element) node;\nint otherwiseCount = choose.getElementsByTagName(\"otherwise\").getLength();\nif (otherwiseCount > 1) throw new IllegalStateException(\"choose has \" + otherwiseCount + \" otherwise blocks\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer <when> chains with explicit tests over multiple otherwise fallbacks","Break very large choose blocks into <sql> fragments to reduce merge-conflict duplication","Review mapper XML diffs carefully after resolving git conflicts"],"tags":["mybatis","dynamic-sql","choose","xml"],"backgroundTag":null,"analyzedSha":"bf67d907478c724120bf76292da54abf9e73c2b3","analyzedAt":"2026-08-14T15:17:09.543Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}