{"record":{"id":"2b09d76eaa7edb10","repo":"hibernate/hibernate-orm","slug":"requested-tuple-value-index-s-realtype-s-cann","errorCode":null,"errorMessage":"Requested tuple value [index=%s, realType=%s] cannot be assigned to requested type [%s]","messagePattern":"Requested tuple value \\[index=(.+?), realType=(.+?)\\] cannot be assigned to requested type \\[(.+?)\\]","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/sql/results/internal/TupleImpl.java","lineNumber":77,"sourceCode":"\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)\n\t\t\t);\n\t\t}\n\t\treturn cast( type, result );\n\t}\n\n\t@Override\n\tpublic Object get(int i) {\n\t\tif ( i >= row.length ) {\n\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\t\"Given index [\" + i + \"] was outside the range of result tuple size [\" + row.length + \"] \"\n\t\t\t);\n\t\t}","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/sql/results/internal/TupleImpl.java#L59-L95","documentation":"Thrown by TupleImpl.get(int i, Class type): the indexed access succeeded but the value's runtime type is not an instance of the requested type. The message prints the index, the real type FQN, and the requested type FQN. Null values skip the check.","triggerScenarios":"`tuple.get(1, Integer.class)` where position 1 holds a Long count; reordering the select list so an index now points at a differently-typed column; assuming String for a database enum or numeric column.","commonSituations":"Index-based readers breaking after someone reorders or inserts a column into the select; type assumptions across dialects (e.g. numeric precision differences); maintenance code that hard-codes both positions and types.","solutions":["Use the real runtime type (print `tuple.get(i).getClass()` once) or a supertype like Number.class","Alias the columns and read by alias so reordering cannot silently shift types","Pin the SQL type explicitly with cast: `cast(sum(e.salary) as integer) as total`"],"exampleFix":"// before\nInteger total = tuple.get(1, Integer.class);   // position 1 is BigDecimal\n// after\njava.math.BigDecimal total = tuple.get(1, java.math.BigDecimal.class);","handlingStrategy":"type-guard","validationCode":"// Inspect the real type at the index before the typed read\nObject raw = tuple.get(i);\nif (raw != null && !type.isInstance(raw)) { /* use raw.getClass() or Number conversion */ }","typeGuard":"static <X> X tupleAt(Tuple t, int i, Class<X> type) {\n    Object v = t.get(i);\n    if (v == null || type.isInstance(v)) return type.cast(v);\n    if (v instanceof Number n && type == Integer.class) return type.cast(n.intValue());\n    if (v instanceof Number n && type == Long.class) return type.cast(n.longValue());\n    throw new ClassCastException(v.getClass() + \" at \" + i + \" -> \" + type);\n}","tryCatchPattern":"catch (IllegalArgumentException e) { if (e.getMessage().contains(\"index=\")) { /* re-read untyped and convert */ } throw e; }","preventionTips":["Prefer alias-based reads; indexes break silently when the select list is reordered","Pin SQL-level types with cast(...) when the consumer needs a specific Java type","Probe tuple types after any select-list change"],"tags":["tuple","jpa","type-mismatch","indexed-access"],"backgroundTag":"tuple-element-access","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}