{"record":{"id":"cb33336ce316d278","repo":"hibernate/hibernate-orm","slug":"encountered-duplicate-alias-for-map-dynamic-instan","errorCode":null,"errorMessage":"Encountered duplicate alias for Map dynamic instantiation argument [{}]","messagePattern":"Encountered duplicate alias for Map dynamic instantiation argument \\[(.+?)\\]","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/sql/results/graph/instantiation/internal/DynamicInstantiationAssemblerMapImpl.java","lineNumber":42,"sourceCode":" */\npublic class DynamicInstantiationAssemblerMapImpl implements DomainResultAssembler<Map<?,?>> {\n\tprivate final JavaType<Map<?,?>> mapJavaType;\n\tprivate final List<ArgumentReader<?>> argumentReaders;\n\n\tpublic DynamicInstantiationAssemblerMapImpl(\n\t\t\tJavaType<Map<?,?>> mapJavaType,\n\t\t\tList<ArgumentReader<?>> argumentReaders) {\n\t\tthis.mapJavaType = mapJavaType;\n\t\tthis.argumentReaders = argumentReaders;\n\n\t\tfinal Set<String> aliases = new HashSet<>();\n\t\tfor ( var argumentReader : argumentReaders ) {\n\t\t\tif ( argumentReader.getAlias() == null ) {\n\t\t\t\tthrow new IllegalStateException( \"alias for Map dynamic instantiation argument cannot be null\" );\n\t\t\t}\n\n\t\t\tif ( ! aliases.add( argumentReader.getAlias() ) ) {\n\t\t\t\tthrow new IllegalStateException( \"Encountered duplicate alias for Map dynamic instantiation argument [\" + argumentReader.getAlias() + \"]\" );\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate DynamicInstantiationAssemblerMapImpl(\n\t\t\tList<ArgumentReader<?>> argumentReaders,\n\t\t\tJavaType<Map<?,?>> mapJavaType) {\n\t\tthis.mapJavaType = mapJavaType;\n\t\tthis.argumentReaders = argumentReaders;\n\t}\n\n\t@Override\n\tpublic JavaType<Map<?,?>> getAssembledJavaType() {\n\t\treturn mapJavaType;\n\t}\n\n\t@Override\n\tpublic Map<?,?> assemble(","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/sql/results/graph/instantiation/internal/DynamicInstantiationAssemblerMapImpl.java#L24-L60","documentation":"Thrown while building the assembler for `select new map(...)`: two or more arguments were given the same alias. Map keys must be unique, so duplicate aliases make the entry set ambiguous. As with error 3180, the equivalent check in DynamicInstantiationResultImpl (error 3183) usually fires earlier at query-plan creation; this message appears via direct assembler construction paths.","triggerScenarios":"`select new map(a.id as x, b.id as x) from A a join a.b b`; copy-pasting an alias when adding a new argument to a map instantiation; aliases generated in a loop in dynamic query building that collide.","commonSituations":"Refactoring a select list by duplicating a line and forgetting to change the alias; HQL built by string concatenation where a variable alias is reused; large map instantiations where duplicate aliases are easy to miss.","solutions":["Make every alias in the `new map(...)` list unique, e.g. `select new map(a.id as aId, b.id as bId)`","Re-run the query - the validation happens before any SQL is executed","If aliases are generated programmatically, suffix them with the expression index to guarantee uniqueness"],"exampleFix":"// before\nselect new map(a.id as id, b.id as id) from A a join a.b b\n// after\nselect new map(a.id as aId, b.id as bId) from A a join a.b b","handlingStrategy":"validation","validationCode":"// Verify aliases are unique before building/running the query\nSet<String> seen = new HashSet<>();\nfor (String arg : args.split(\",\")) {\n    String alias = arg.trim().replaceFirst(\"(?is).*\\\\bas\\\\s+\", \"\").trim();\n    if (!seen.add(alias)) throw new IllegalArgumentException(\"Duplicate alias in new map(...): \" + alias);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["When copy-pasting a select line into a map instantiation, change the alias first","For generated HQL, derive aliases from expression + position (e.g. col_0, col_1)","Code-review rule: map instantiations must have as many distinct aliases as arguments"],"tags":["hql","dynamic-instantiation","map","alias","duplicate-alias"],"backgroundTag":"hql-dynamic-instantiation","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}