{"record":{"id":"f7da64a2d1d396a8","repo":"mybatis/mybatis-3","slug":"environment-requires-an-id-attribute","errorCode":null,"errorMessage":"Environment requires an id attribute.","messagePattern":"Environment requires an id attribute\\.","errorType":"validation","errorClass":"BuilderException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/builder/xml/XMLConfigBuilder.java","lineNumber":430,"sourceCode":"            mapperParser.parse();\n          }\n        } else if (resource == null && url == null && mapperClass != null) {\n          Class<?> mapperInterface = Resources.classForName(mapperClass);\n          configuration.addMapper(mapperInterface);\n        } else {\n          throw new BuilderException(\n              \"A mapper element may only specify a url, resource or class, but not more than one.\");\n        }\n      }\n    }\n  }\n\n  private boolean isSpecifiedEnvironment(String id) {\n    if (environment == null) {\n      throw new BuilderException(\"No environment specified.\");\n    }\n    if (id == null) {\n      throw new BuilderException(\"Environment requires an id attribute.\");\n    }\n    return environment.equals(id);\n  }\n\n  private static Configuration newConfig(Class<? extends Configuration> configClass) {\n    try {\n      return configClass.getDeclaredConstructor().newInstance();\n    } catch (Exception ex) {\n      throw new BuilderException(\"Failed to create a new Configuration instance.\", ex);\n    }\n  }\n\n}\n","sourceCodeStart":412,"sourceCodeEnd":444,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/builder/xml/XMLConfigBuilder.java#L412-L444","documentation":"Thrown by XMLConfigBuilder.isSpecifiedEnvironment() when an <environment> child element inside <environments> lacks the id attribute. Environments are identified purely by id (e.g. 'development', 'production'), and the builder needs it to match against the default environment, so a missing id is fatal during config parsing.","triggerScenarios":"An <environment> element with transactionManager/dataSource children but no id attribute, e.g. <environment> instead of <environment id=\"development\">. Raised while SqlSessionFactoryBuilder.build() walks the environments section.","commonSituations":"Copying an <environment> block to create a second environment and forgetting to add the new id; XML schema validation disabled so the missing required attribute is not caught earlier; refactoring that strips attributes.","solutions":["Add a unique id attribute to every <environment> element","Ensure the id matches the default attribute of <environments> for at least one environment","Enable XSD validation in your IDE against mybatis-3-config.xsd to catch this at edit time"],"exampleFix":"<!-- before -->\n<environment>\n  <transactionManager type=\"JDBC\"/>\n</environment>\n\n<!-- after -->\n<environment id=\"development\">\n  <transactionManager type=\"JDBC\"/>\n</environment>","handlingStrategy":"validation","validationCode":"for (Element env : environmentChildren) {\n  if (env.getAttribute(\"id\") == null || env.getAttribute(\"id\").isEmpty())\n    throw new IllegalStateException(\"<environment> missing required id attribute\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat id as mandatory when authoring environments","Use XSD-aware editors so missing required attributes are flagged"],"tags":["mybatis","xml-config","environment"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}