mybatis/mybatis-3 · error · BuilderException
Wrong namespace. Expected '{currentNamespace}' but found '{c
Error message
Wrong namespace. Expected '{currentNamespace}' but found '{currentNamespace}'. What it means
setCurrentNamespace() enforces that one MapperBuilderAssistant instance (one mapper resource) is bound to exactly one namespace. If a different namespace string arrives after the first was set - nested <mapper> definitions, multiple root elements, or parser reuse across files - MyBatis throws this BuilderException showing expected vs found.
Source
Thrown at src/main/java/org/apache/ibatis/builder/MapperBuilderAssistant.java:81
private boolean unresolvedCacheRef; // issue #676
public MapperBuilderAssistant(Configuration configuration, String resource) {
super(configuration);
ErrorContext.instance().resource(resource);
this.resource = resource;
}
public String getCurrentNamespace() {
return currentNamespace;
}
public void setCurrentNamespace(String currentNamespace) {
if (currentNamespace == null) {
throw new BuilderException("The mapper element requires a namespace attribute to be specified.");
}
if (this.currentNamespace != null && !this.currentNamespace.equals(currentNamespace)) {
throw new BuilderException(
"Wrong namespace. Expected '" + this.currentNamespace + "' but found '" + currentNamespace + "'.");
}
this.currentNamespace = currentNamespace;
}
public String applyCurrentNamespace(String base, boolean isReference) {
if (base == null) {
return null;
}
if (isReference) {
// is it qualified with any namespace yet?
if (base.contains(".")) {
return base;
}
} else {
// is it qualified with this namespace yet?
if (base.startsWith(currentNamespace + ".")) {View on GitHub (pinned to 008069adb1)
Solutions
- Inspect the mapper XML named in the error context: ensure exactly one root <mapper> with one namespace attribute
- Split statements belonging to different namespaces into separate files, one namespace per file
- If generating XML, ensure each emitted file has a single root element and a fixed namespace
Example fix
<!-- before (one file, two roots after bad merge) --> <mapper namespace="com.example.A">...</mapper> <mapper namespace="com.example.B">...</mapper> <!-- after --> <!-- file A.xml --> <mapper namespace="com.example.A">...</mapper> <!-- file B.xml --> <mapper namespace="com.example.B">...</mapper>
Defensive patterns
Strategy: validation
Validate before calling
// CI check: each mapper file has exactly one root <mapper> element and one namespace // DocumentBuilderFactory is namespace-aware; reject files with >1 <mapper> root child
Prevention
- One namespace per XML file; split multi-namespace files
- Review merge conflicts in mapper XML carefully - duplicated roots are a common artifact
- If generating mappers, emit one file per namespace
When it happens
Trigger: An XML file containing statements that applyCurrentNamespace resolves to a second namespace, e.g. cross-file references during incomplete parsing; an embedded mapper element inside another mapper file; tools that reuse the same assistant for multiple mapper files; XML structure issues (duplicated root elements after a bad merge).
Common situations: Bad merges leaving two <mapper> roots in one file; code generators emitting nested mapper elements; custom XML parsing pipelines reusing builder assistants.
Related errors
- The mapper element requires a namespace attribute to be spec
- Error resolving JdbcType. Cause: {cause}
- Error resolving ResultSetType. Cause: {cause}
- Error resolving ParameterMode. Cause: {cause}
- Error creating instance. Cause: {cause}
AI-assisted analysis of mybatis/mybatis-3@008069adb1 (2026-08-14).
Data as JSON: /api/errors/e0f3b6f70670ca44.
Report an issue: GitHub.