{"record":{"id":"df5a26de3a102edc","repo":"hibernate/hibernate-orm","slug":"root-path-treats-can-not-be-fetched-getnavig","errorCode":null,"errorMessage":"Root path treats can not be fetched - \" + getNavigablePath().getFullPath()","messagePattern":"Root path treats can not be fetched - \" \\+ getNavigablePath\\(\\)\\.getFullPath\\(\\)","errorType":"exception","errorClass":"TreatException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/from/SqmRoot.java","lineNumber":255,"sourceCode":"\t@Nonnull\n\tpublic <S extends E> SqmTreatedFrom<E,E,S> treatAs(@Nonnull EntityDomainType<S> treatTarget, @Nullable String alias) {\n\t\treturn treatAs( treatTarget, alias, false );\n\t}\n\n\t@Override\n\t@Nonnull\n\tpublic <S extends E> SqmTreatedFrom<E,E,S> treatAs(@Nonnull Class<S> treatJavaType, @Nullable String alias, boolean fetch) {\n\t\treturn treatAs( nodeBuilder().getDomainModel().entity( treatJavaType ), alias, fetch );\n\t}\n\n\t@Override\n\t@Nonnull\n\tpublic <S extends E> SqmTreatedFrom<E,E,S> treatAs(@Nonnull EntityDomainType<S> treatTarget, @Nullable String alias, boolean fetch) {\n\t\tif ( alias != null ) {\n\t\t\tthrow new TreatException( \"Root path treats can not be aliased - \" + getNavigablePath().getFullPath() );\n\t\t}\n\t\tif ( fetch ) {\n\t\t\tthrow new TreatException( \"Root path treats can not be fetched - \" + getNavigablePath().getFullPath() );\n\t\t}\n\t\tfinal var treat = findTreat( treatTarget, null );\n\t\tif ( treat == null ) {\n\t\t\tfinal var treatedRoot = new SqmTreatedRoot<>( this, (SqmEntityDomainType<S>) treatTarget );\n\t\t\t@SuppressWarnings(\"unchecked\")\n\t\t\tfinal var typedTreat = (SqmTreatedFrom<E, E, S>) treatedRoot;\n\t\t\treturn addTreat( typedTreat );\n\t\t}\n\t\treturn treat;\n\t}\n\n}\n","sourceCodeStart":237,"sourceCodeEnd":268,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/from/SqmRoot.java#L237-L268","documentation":"SqmRoot.treatAs(EntityDomainType, alias, fetch) throws TreatException when fetch=true. A root treat is a type-narrowing expression, not a fetch graph element: SQL TREAT does not fetch anything itself, and Hibernate cannot generate fetch joins for a virtual treated root on top of the existing root row. Root treats are only valid as unaliased, unfetched path sources; fetching must be expressed as explicit fetch joins.","triggerScenarios":"Criteria: `root.treatAs( Sub.class, null, true )` — the three-arg overload with fetched=true. HQL constructs that translate to a fetched root treat, e.g. `join fetch treat(p as Sub).attr` on a root, which sets the fetched flag on SqmRoot.treatAs. Generic code that always passes fetched=true.","commonSituations":"Trying to combine TREAT with JOIN FETCH at the root level (`from Person p join fetch treat(p as Sub).specialAttr`); building generic fetch-graph resolvers that mark every treated path as fetched; upgrading from older Hibernate versions where this pattern was silently accepted or failed later.","solutions":["Use `root.treatAs( Sub.class )` without fetch, and add explicit fetch joins on the treated path: `join fetch p.subAttr`","Move the fetch to an association join and treat that join: `join p.related r fetch` + `treat(r as Sub)`","Select the subtype attributes you need instead of fetching them","Restructure the query as a subtype-targeted query (`from SpecialPerson`) when you only want that subtype with its associations"],"exampleFix":"// before (HQL: root treat + fetch)\nList<Person> l = session.createQuery(\"from Person p join fetch treat(p as SpecialPerson).bonus\", Person.class).list();\n\n// after\nList<SpecialPerson> l = session.createQuery(\"from SpecialPerson s join fetch s.bonus\", SpecialPerson.class).list();","handlingStrategy":"validation","validationCode":"boolean fetched = false; // root treats can never be fetched\nroot.treatAs( Sub.class, null, fetched );\n// or simply: root.treatAs( Sub.class );","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never combine JOIN FETCH with TREAT at the root level","Express fetching through explicit fetch joins on associations","Query the concrete subtype directly when its associations must be fetched"],"tags":["hibernate","hql","criteria-api","treat-as","fetch","semantic-error"],"backgroundTag":"jpa-treat-as-unsupported","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}