{"record":{"id":"6faea93d43108eb6","repo":"hibernate/hibernate-orm","slug":"requested-tuple-value-alias-s-value-s-cannot","errorCode":null,"errorMessage":"Requested tuple value [alias=%s, value=%s] cannot be assigned to requested type [%s]","messagePattern":"Requested tuple value \\[alias=(.+?), value=(.+?)\\] 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":48,"sourceCode":"\n\t@Override\n\tpublic <X> X get(TupleElement<X> tupleElement) {\n\t\tfinal Integer index = tupleMetadata.get( tupleElement );\n\t\tif ( index == null ) {\n\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\t\"Requested tuple element did not correspond to 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 cast( tupleElement.getJavaType(), row[index] );\n\t}\n\n\t@Override\n\tpublic <X> X get(String alias, Class<X> type) {\n\t\tfinal Object untyped = get( alias );\n\t\tif ( untyped != null ) {\n\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\"","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/sql/results/internal/TupleImpl.java#L30-L66","documentation":"Thrown by TupleImpl.get(String alias, Class type): the alias resolved fine, but the stored value's runtime type is not an instance of the requested type. The message prints the alias, the actual value, and the requested class FQN, so the mismatch is directly readable. Null values skip the check and are returned as null.","triggerScenarios":"`tuple.get(\"count\", Integer.class)` when the database/Hibernate returns Long for count aggregates; `tuple.get(\"id\", Integer.class)` for an id mapped as Long; requesting a specific Number subclass (Integer/BigDecimal) different from what the dialect returns.","commonSituations":"Assuming Integer for SQL counts and ids that Hibernate maps as Long; dialect differences in numeric type resolution between test and production databases; requesting a value type that changed after a schema or mapping change.","solutions":["Request the type Hibernate actually produces (Long for ids/counts), or a common supertype (`Number.class`) and convert yourself","Fix the select expression type: `cast(count(e) as integer)` in HQL","After any schema or dialect change, dump `tuple.get(alias).getClass()` once to re-learn the runtime types"],"exampleFix":"// before\nint count = tuple.get(\"cnt\", Integer.class);   // Long stored\n// after\nlong count = tuple.get(\"cnt\", Long.class);","handlingStrategy":"type-guard","validationCode":"// Check assignability before the typed get\nObject raw = tuple.get(alias);\nClass<?> want = Long.class;\nif (raw != null && !want.isInstance(raw)) { /* read as Number and convert */ }","typeGuard":"static <X> X tupleGet(Tuple t, String alias, Class<X> type) {\n    Object v = t.get(alias);\n    if (v == null) return null;\n    if (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() + \" -> \" + type);\n}","tryCatchPattern":"catch (IllegalArgumentException e) { if (e.getMessage().contains(\"cannot be assigned to requested type\")) { /* read as Number.class and convert */ } throw e; }","preventionTips":["Default to Number.class for numeric tuple reads and convert explicitly","Assert the runtime tuple types in a probe test; they differ from SQL types (count -> Long)","After dialect or mapping changes re-learn the types instead of trusting old assumptions"],"tags":["tuple","jpa","type-mismatch","numeric-types"],"backgroundTag":"tuple-element-access","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}