{"record":{"id":"fb63dc697acfe0d6","repo":"hibernate/hibernate-orm","slug":"cannot-correlate-from-node-parentfrom","errorCode":null,"errorMessage":"Cannot correlate from node [${parentFrom}]","messagePattern":"Cannot correlate from node \\[(.+?)\\]","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/select/SqmSubQuery.java","lineNumber":540,"sourceCode":"\n\t@Nonnull\n\t@Override\n\tpublic <X, Y> SqmFrom<X, Y> correlate(@Nonnull From<X, Y> parentFrom) {\n\t\tif ( parentFrom instanceof Root<?> ) {\n\t\t\t//noinspection unchecked\n\t\t\treturn (SqmFrom<X, Y>) correlate( (Root<Y>) parentFrom );\n\t\t}\n\t\telse if ( parentFrom instanceof Join<?, ?> ) {\n\t\t\treturn correlate( (Join<X, Y>) parentFrom );\n\t\t}\n\t\telse if ( parentFrom instanceof JpaCrossJoin<?, ?> ) {\n\t\t\treturn (SqmFrom<X, Y>) correlate( (JpaCrossJoin<X, Y>) parentFrom );\n\t\t}\n\t\telse if ( parentFrom instanceof JpaEntityJoin<?, ?> ) {\n\t\t\treturn (SqmFrom<X, Y>) correlate( (JpaEntityJoin<T, Y>) parentFrom );\n\t\t}\n\t\telse {\n\t\t\tthrow new IllegalArgumentException( \"Cannot correlate from node [\" + parentFrom + \"]\" );\n\t\t}\n\t}\n\n\t@Nonnull\n\t@Override\n\tpublic <X, Y> SqmCorrelatedJoin<X, Y> correlate(@Nonnull Join<X, Y> join) {\n\t\tif ( join instanceof PluralJoin<?, ?, ?> pluralJoin ) {\n\t\t\treturn switch ( pluralJoin.getModel().getCollectionType() ) {\n\t\t\t\tcase COLLECTION -> correlate( (CollectionJoin<X, Y>) join );\n\t\t\t\tcase LIST -> correlate( (ListJoin<X, Y>) join );\n\t\t\t\tcase SET -> correlate( (SetJoin<X, Y>) join );\n\t\t\t\tcase MAP -> correlate( (MapJoin<X, ?, Y>) join );\n\t\t\t};\n\t\t}\n\t\tfinal SqmCorrelatedSingularValuedJoin<X, Y> correlated =\n\t\t\t\t( (SqmSingularValuedJoin<X, Y>) join ).createCorrelation();\n\t\tgetQuerySpec().addRoot( correlated.getCorrelatedRoot() );\n\t\treturn correlated;","sourceCodeStart":522,"sourceCodeEnd":558,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/select/SqmSubQuery.java#L522-L558","documentation":"The one-arg correlate(From) is a dispatcher: it only knows how to correlate parent From nodes that are a Root, a Join, a JpaCrossJoin, or a JpaEntityJoin. Any other From implementation — e.g. a CTE root (SqmCteRoot), a derived/dynamic-instantiation root, or some other synthetic node — cannot be brought into the subquery and throws IllegalArgumentException naming the node.","triggerScenarios":"subquery.correlate(fromNode) where fromNode is a root obtained from a CTE (query.with(...) then cteRoot), a SqmDerivedRoot from a dynamic instantiation, or any non-standard From you received from generic traversal code rather than from query.from(Entity.class) / joins.","commonSituations":"CTE-based criteria queries where the developer correlates the CTE root into a subquery instead of an entity root; generic frameworks that accept an arbitrary From and call correlate() on it; refactors that changed which root a subquery correlates.","solutions":["Correlate the underlying entity Root or a concrete Join instead of the CTE/derived root","Inside the subquery, reference the CTE by joining to it (cb cross/entity join or JpaCteCriteria attribute join) rather than correlating it","If you handle From nodes generically, branch on Root/Join/JpaCrossJoin/JpaEntityJoin and fail with your own clear message for other kinds"],"exampleFix":"// before\nJpaCteCriteria<Long> cte = query.with(\"totals\", ...);\nRoot<?> cteRoot = (Root<?>) cte; // synthetic root\nsub.correlate(cteRoot); // IllegalArgumentException: Cannot correlate from node\n\n// after\nRoot<Order> orderRoot = query.from(Order.class);\nsub.correlate(orderRoot); // correlate a real entity root\nsub.select(...).where(cb.equal(subRoot.get(\"id\"), orderRoot.get(\"id\")));","handlingStrategy":"type-guard","validationCode":"if (from instanceof Root || from instanceof Join || from instanceof JpaCrossJoin || from instanceof JpaEntityJoin) { sub.correlate(from); } else { throw new IllegalArgumentException(\"Unsupported From node for correlation: \" + from); }","typeGuard":"static boolean isCorrelatable(From<?, ?> from) {\n    return from instanceof Root || from instanceof Join\n            || from instanceof JpaCrossJoin || from instanceof JpaEntityJoin;\n}","tryCatchPattern":"try { sub.correlate(from); } catch (IllegalArgumentException e) { if (e.getMessage().startsWith(\"Cannot correlate\")) { /* correlate the underlying entity root instead */ } else throw e; }","preventionTips":["Only correlate roots and joins obtained from query.from(Entity.class) or explicit joins","For CTE roots, join to the CTE inside the subquery rather than correlating it","Type generic correlation helpers against Root/Join, not the raw From interface"],"tags":["hibernate","criteria-api","correlation","subquery","cte"],"backgroundTag":"unsupported-correlation-source","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}