{"record":{"id":"4b97c576ccc6c72a","repo":"hibernate/hibernate-orm","slug":"requested-tuple-element-did-not-correspond-to-elem","errorCode":null,"errorMessage":"Requested tuple element did not correspond to element in the result tuple","messagePattern":"Requested tuple element did not correspond to 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":35,"sourceCode":"/**\n * Implementation of the JPA Tuple contract\n *\n * @author Steve Ebersole\n */\npublic class TupleImpl implements Tuple {\n\tprivate final TupleMetadata tupleMetadata;\n\tprivate final Object[] row;\n\n\tpublic TupleImpl(TupleMetadata tupleMetadata, Object[] row) {\n\t\tthis.tupleMetadata = tupleMetadata;\n\t\tthis.row = row;\n\t}\n\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()","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/sql/results/internal/TupleImpl.java#L17-L53","documentation":"Thrown by TupleImpl.get(TupleElement) when the passed TupleElement is not registered in this tuple's TupleMetadata. TupleElement handles are only meaningful for the query that produced them; each execution builds its own metadata keyed by alias. The check is an identity/lookup failure, not a type failure - the element simply does not belong to this result tuple.","triggerScenarios":"Caching a TupleElement from query A and calling `tupleFromB.get(cachedElement)`; implementing a custom TupleElement and passing it in; comparing elements across two executions of the same query whose aliases differ (e.g. generated alias changes).","commonSituations":"Helper utilities that accept `(Tuple, TupleElement)` pairs assembled from different sources; refactoring that separates tuple creation from element definition; tests that mock Tuple/TupleElement inconsistently.","solutions":["Get values by alias or index instead: `tuple.get(\"id\", Long.class)` or `tuple.get(0)`","Only use TupleElement handles obtained from that same tuple: `tuple.getElements().get(i)`","Pass aliases (String) between components rather than TupleElement objects"],"exampleFix":"// before\nX v = someTuple.get(elementFromEarlierQuery);\n// after\nX v = someTuple.get(elementFromEarlierQuery.getAlias(), (Class<X>) elementFromEarlierQuery.getJavaType());","handlingStrategy":"validation","validationCode":"// Only use element handles that belong to this tuple\nboolean known = tuple.getElements().stream()\n        .anyMatch(el -> el.getAlias().equals(element.getAlias())\n                && el.getJavaType() == element.getJavaType());\nif (!known) { /* fall back to alias lookup */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass alias strings (not TupleElement objects) between components","Always obtain TupleElement from tuple.getElements() of the same execution","Avoid implementing custom TupleElement types; you gain nothing and break lookup"],"tags":["tuple","jpa","query-result","tuple-element"],"backgroundTag":"tuple-element-access","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}