{"record":{"id":"46002146e6de2a96","repo":"hibernate/hibernate-orm","slug":"derived-roots-can-not-be-treated","errorCode":null,"errorMessage":"Derived roots can not be treated","messagePattern":"Derived roots can not be treated","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/domain/SqmDerivedRoot.java","lineNumber":116,"sourceCode":"\tpublic String getEntityName() {\n\t\tthrow new UnsupportedOperationException( \"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\t@Override\n\t@Nonnull\n\tpublic SqmCorrelatedRoot<T> createCorrelation() {\n\t\treturn new SqmCorrelatedDerivedRoot<>( this );\n\t}\n\n\t@Override\n\t@Nonnull\n\tpublic <S extends T> SqmTreatedFrom<T, T, S> treatAs(@Nonnull EntityDomainType<S> treatTarget, @Nullable String alias, boolean fetch) {\n\t\tthrow new UnsupportedOperationException( \"Derived roots can not be treated\" );\n\t}\n\n\t@Override\n\tpublic boolean deepEquals(SqmFrom<?, ?> object) {\n\t\treturn super.deepEquals( object )\n\t\t\t&& subQuery.equals( ((SqmDerivedRoot<?>) object).subQuery );\n\t}\n\n\t@Override\n\tpublic boolean isDeepCompatible(SqmFrom<?, ?> object) {\n\t\treturn super.isDeepCompatible( object )\n\t\t\t&& subQuery.isCompatible( ((SqmDerivedRoot<?>) object).subQuery );\n\t}\n}\n","sourceCodeStart":98,"sourceCodeEnd":131,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/domain/SqmDerivedRoot.java#L98-L131","documentation":"SqmDerivedRoot.treatAs throws UnsupportedOperationException: TREAT requires an entity inheritance hierarchy, but a derived root denotes subquery rows with no persistent type hierarchy, so downcasting it is meaningless. This is the derived-root counterpart of the basic-path treat failures. The exception is thrown while the SQM tree is built, before SQL rendering.","triggerScenarios":"HQL \"select treat(t as Manager).bonus from (select e from Employee e) t\"; criteria code calling derivedRoot.treatAs(Manager.class) on a root created for a from-subquery; copy of entity-root treat patterns applied to derived roots.","commonSituations":"Queries rewritten over derived tables (for window functions, row limiting) that kept TREAT from the original entity query; migration from Blaze-Persistence where treating subselect aliases behaved differently; assumption that \"from (select e from Employee e)\" preserves treatability of e.","solutions":["Apply TREAT inside the inner subquery before deriving: \"from (select treat(e as Manager) m from Employee e) t\" select m.bonus from t.","Or treat the entity root directly in an ordinary query and push filtering into a where clause instead of a derived table.","If only subclass rows matter, filter with a type() predicate in the inner select: \"select e from Employee e where type(e) = Manager\".","Avoid treatAs on any root that is not a plain SqmRoot/SqmEntityJoin."],"exampleFix":"// before - derived root cannot be treated\n\"select treat(t as Manager).bonus from (select e from Employee e) t\"\n\n// after - treat inside the derived subquery first\n\"select t.bonus from (select treat(e as Manager) as m from Employee e where type(e) = Manager) t\"","handlingStrategy":"type-guard","validationCode":"// Only emit TREAT when the treated node is a plain entity root/join\nstatic boolean treatableRoot(Object node) {\n    return node instanceof org.hibernate.query.sqm.tree.spi.from.SqmRoot\n        || node instanceof org.hibernate.query.sqm.tree.spi.from.SqmEntityJoin\n        || node instanceof org.hibernate.query.sqm.tree.spi.from.SqmCrossJoin;\n    // note: SqmDerivedRoot, SqmCteRoot, SqmCorrelatedDerivedRoot throw on treatAs\n}","typeGuard":"static boolean isDerivedRoot(Object node) {\n    return node instanceof org.hibernate.query.sqm.tree.spi.domain.SqmDerivedRoot;\n}","tryCatchPattern":"try {\n    root.treatAs(target, alias, fetch);\n} catch (UnsupportedOperationException e) {\n    throw new IllegalArgumentException(\"TREAT is not supported on derived roots; move treat into the inner subquery\", e);\n}","preventionTips":["When refactoring entity queries into from-subquery form, move TREAT inside the subquery.","Use type(e) = Subclass predicates in the inner select as an alternative to treat.","Static-check for treatAs( on roots whose query contains 'from (' during code review."],"tags":["hibernate","hql","sqm","treat","derived-root","subquery"],"backgroundTag":"hql-treat-downcast-unsupported","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}