{"record":{"id":"74a2abb083b96990","repo":"hibernate/hibernate-orm","slug":"correlated-derived-root-does-not-have-an-entity-ty-74a2ab","errorCode":null,"errorMessage":"Correlated derived root does not have an entity type. Use getReferencedPathSource() instead.","messagePattern":"Correlated derived root does not have an entity type\\. Use getReferencedPathSource\\(\\) instead\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/domain/SqmCorrelatedDerivedRootJoin.java","lineNumber":81,"sourceCode":"\t\t\t);\n\t\t}\n\t\trootJoin.addSqmJoin( correlatedJoin );\n\t\treturn rootJoin;\n\t}\n\n\t@Override\n\tpublic boolean containsOnlyInnerJoins() {\n\t\t// The derived join is just referenced, no need to create any table groups\n\t\treturn true;\n\t}\n\n\t// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\t// JPA\n\n\t@Nonnull\n\t@Override\n\tpublic SqmEntityDomainType<T> getModel() {\n\t\tthrow new UnsupportedOperationException( \"Correlated derived root does not have an entity type. Use getReferencedPathSource() instead.\" );\n\t}\n\n\t@Override\n\tpublic String getEntityName() {\n\t\tthrow new UnsupportedOperationException( \"Correlated derived root does not have an entity type. Use getReferencedPathSource() instead.\" );\n\t}\n\n\t@Override\n\tpublic SqmPathSource<T> getResolvedModel() {\n\t\treturn getReferencedPathSource();\n\t}\n}\n","sourceCodeStart":63,"sourceCodeEnd":94,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/domain/SqmCorrelatedDerivedRootJoin.java#L63-L94","documentation":"SqmCorrelatedDerivedRootJoin is the join node created when an inner query correlates a join whose referent is a derived (subquery-based) join. Like the correlated derived root, it wraps query-derived rows, so getModel() - the JPA From method returning an EntityType - throws UnsupportedOperationException. The message directs you to getReferencedPathSource() for the actual row type.","triggerScenarios":"Criteria code calling subquery.correlate(derivedJoin) and then .getModel() on the returned join; HQL where a subquery references an outer \"join (select ...) d\" and Java code walks the subquery joins asking for their entity models; framework code that resolves EntityType from every From element to plan fetches or builds joins.","commonSituations":"Fetch/join planners in repository abstractions that iterate all From elements and call getModel(); queries refactored to join derived tables or CTEs while join-planning code assumed only entity joins; Hibernate version upgrades that introduced correlated derived join support and changed which node types generic code receives.","solutions":["Call getReferencedPathSource() / getResolvedModel() on the correlated derived join instead of getModel().","Type-check each From element (skip SqmCorrelatedDerivedRootJoin / SqmDerivedJoin) before requesting entity metadata in generic code.","Rewrite the correlated join against the underlying entity when a true EntityType is required for the plan.","Cover join-walking utilities with tests that include derived joins."],"exampleFix":"// before - fails for a join correlated from a derived join\nEntityType<?> et = (EntityType<?>) correlatedJoin.getModel();\n\n// after - read the referenced path source of the derived join\nSqmPathSource<?> src = ((SqmCorrelatedDerivedRootJoin<?>) correlatedJoin).getReferencedPathSource();","handlingStrategy":"type-guard","validationCode":"static boolean hasEntityType(jakarta.persistence.criteria.From<?, ?> from) {\n    return !(from instanceof org.hibernate.query.sqm.tree.spi.domain.SqmCorrelatedDerivedRootJoin)\n        && !(from instanceof org.hibernate.query.sqm.tree.spi.domain.SqmDerivedJoin)\n        && !(from instanceof org.hibernate.query.sqm.tree.spi.domain.SqmCteJoin);\n}","typeGuard":"static boolean isEntityJoin(Object fromNode) {\n    return fromNode instanceof org.hibernate.query.sqm.tree.spi.from.SqmEntityJoin\n        || fromNode instanceof org.hibernate.query.sqm.tree.spi.domain.SqmAttributeJoin<?, ?> a\n            && !(fromNode instanceof org.hibernate.query.sqm.tree.spi.domain.SqmDerivedJoin);\n}","tryCatchPattern":"try {\n    EntityType<?> t = (EntityType<?>) from.getModel();\n} catch (UnsupportedOperationException e) {\n    SqmPathSource<?> src = ((org.hibernate.query.sqm.tree.spi.domain.SqmCorrelatedDerivedRootJoin<?>) from)\n            .getReferencedPathSource();\n    // proceed with path-source metadata\n}","preventionTips":["Join-planning code must distinguish derived joins from association joins.","Use getResolvedModel() for uniform metadata access across join kinds.","Test join walkers with a subquery correlating a derived join."],"tags":["hibernate","sqm","criteria-api","derived-join","correlation","subquery"],"backgroundTag":"derived-root-no-entity-type","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}