{"record":{"id":"57cab85102664175","repo":"hibernate/hibernate-orm","slug":"given-alias-did-not-correspond-to-an-element","errorCode":null,"errorMessage":"Given alias [{}] did not correspond to an element in the result tuple","messagePattern":"Given alias \\[(.+?)\\] did not correspond to an element in the result tuple","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/sql/results/internal/TupleImpl.java","lineNumber":65,"sourceCode":"\t\t\tif ( !isInstance( type, untyped ) ) {\n\t\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\t\tString.format(\n\t\t\t\t\t\t\t\t\"Requested tuple value [alias=%s, value=%s] cannot be assigned to requested type [%s]\",\n\t\t\t\t\t\t\t\talias,\n\t\t\t\t\t\t\t\tuntyped,\n\t\t\t\t\t\t\t\ttype.getName()\n\t\t\t\t\t\t)\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\treturn cast( type, untyped );\n\t}\n\n\t@Override\n\tpublic Object get(String alias) {\n\t\tfinal Integer index = tupleMetadata.get( alias );\n\t\tif ( index == null ) {\n\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\t\"Given alias [\" + alias + \"] did not correspond to an element in the result tuple\"\n\t\t\t);\n\t\t}\n\t\t// index should be \"in range\" by nature of size check in ctor\n\t\treturn row[index];\n\t}\n\n\t@Override\n\tpublic <X> X get(int i, Class<X> type) {\n\t\tfinal Object result = get( i );\n\t\tif ( result != null && !isInstance( type, result ) ) {\n\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\tString.format(\n\t\t\t\t\t\t\t\"Requested tuple value [index=%s, realType=%s] cannot be assigned to requested type [%s]\",\n\t\t\t\t\t\t\ti,\n\t\t\t\t\t\t\tresult.getClass().getName(),\n\t\t\t\t\t\t\ttype.getName()\n\t\t\t\t\t)","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/sql/results/internal/TupleImpl.java#L47-L83","documentation":"Thrown by TupleImpl.get(String alias): no selection in this result tuple is registered under that alias. Aliases come from the query's select list; expressions selected without an explicit alias get Hibernate-generated aliases (typically positional strings like \"0\", \"1\"), so hand-guessed names for unaliased columns miss.","triggerScenarios":"`tuple.get(\"total\")` when the query says `select sum(e.salary) from Employee e` with no `as total`; alias typo or case difference from the query text; querying by an alias that only exists in a different query variant.","commonSituations":"Reading tuple results from queries whose select list changed; unaliased native/HQL selections where the developer assumed the column name would be the alias; copy-pasting tuple-reading code between similar queries.","solutions":["Add explicit aliases in the select list: `select sum(e.salary) as total ...` then `tuple.get(\"total\")`","Discover the real available aliases once via `tuple.getElements()` and use those names","For positional access use `tuple.get(0)`"],"exampleFix":"// before\nselect sum(e.salary) from Employee e  ...  tuple.get(\"total\")\n// after\nselect sum(e.salary) as total from Employee e  ...  tuple.get(\"total\")","handlingStrategy":"validation","validationCode":"// Validate the alias against this tuple's real element names before reading\nSet<String> names = tuple.getElements().stream().map(TupleElement::getAlias).collect(java.util.stream.Collectors.toSet());\nif (!names.contains(wantedAlias)) { /* log available names, use generated positional alias or fix query */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always alias every selected expression in queries whose results are read as Tuple","Use tuple.getElements() during development to discover generated alias names","Read by index when aliases are unstable, or by alias when they are explicit"],"tags":["tuple","jpa","alias","query-result"],"backgroundTag":"tuple-element-access","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}