{"record":{"id":"0b90bfdaa74acea5","repo":"hibernate/hibernate-orm","slug":"alias-for-map-dynamic-instantiation-argument-canno","errorCode":null,"errorMessage":"alias for Map dynamic instantiation argument cannot be null","messagePattern":"alias for Map dynamic instantiation argument cannot be null","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/sql/results/graph/instantiation/internal/DynamicInstantiationAssemblerMapImpl.java","lineNumber":38,"sourceCode":" * A QueryResultAssembler implementation representing handling for dynamic-\n * instantiations targeting a Map, e.g.`select new map( pers.name, pers.dateOfBirth ) ...`\n *\n * @author Steve Ebersole\n */\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;","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/sql/results/graph/instantiation/internal/DynamicInstantiationAssemblerMapImpl.java#L20-L56","documentation":"Thrown while building the result assembler for an HQL dynamic Map instantiation (`select new map(...)`). Each selected argument becomes one map entry, keyed by that argument's alias, so every argument must carry an `as <alias>`. A null alias means an argument was selected without an alias and therefore has no map key. The higher-level check in DynamicInstantiationResultImpl (error 3182) normally fires first during query-plan creation, so this assembler-level message surfaces only through paths that construct DynamicInstantiationAssemblerMapImpl directly.","triggerScenarios":"Running `select new map(e.id, e.name) from Employee e` (no `as` clauses); `select new map(count(e), sum(e.salary)) from Employee e` (aggregate expressions without aliases); programmatically building an SQM select with DynamicInstantiationNature.MAP while one SqmSelection has a null alias.","commonSituations":"Porting a query from `new list(...)` (aliases optional) to `new map(...)` (aliases mandatory); adding an extra unaliased expression to an existing map instantiation during refactoring; queries written against Hibernate 5 where the failure surfaced at a different layer with a different message.","solutions":["Add a unique alias to every argument: `select new map(e.id as id, e.name as name) from Employee e`","Re-run the query; the check happens at plan creation, so fixing the HQL is sufficient","If building the SQM tree programmatically, call `selection.alias(\"...\")` on every selection before execution"],"exampleFix":"// before\nselect new map(e.id, e.name) from Employee e\n// after\nselect new map(e.id as id, e.name as name) from Employee e","handlingStrategy":"validation","validationCode":"// Before executing, assert every selection in the map instantiation carries an alias (HQL smoke check)\nString hql = \"select new map(e.id as id, e.name as name) from Employee e\";\nString args = hql.substring(hql.indexOf('(') + 1, hql.lastIndexOf(')'));\nfor (String arg : args.split(\",\")) {\n    if (!arg.trim().matches(\"(?is).*\\\\bas\\\\s+\\\\w+\\\\s*$\")) {\n        throw new IllegalArgumentException(\"Unaliased argument in new map(...): \" + arg);\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Alias every argument the moment you write `new map(...)` - treat `as` as mandatory syntax","Keep a startup/integration test per named query so missing aliases fail the build, not production","Prefer explicit aliases over relying on generated ones for any dynamic instantiation"],"tags":["hql","dynamic-instantiation","map","alias","select-new-map","query-plan"],"backgroundTag":"hql-dynamic-instantiation","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}