{"record":{"id":"a2b7aae0250518c8","repo":"hibernate/hibernate-orm","slug":"entity-join-treats-can-not-be-aliased","errorCode":null,"errorMessage":"Entity join treats can not be aliased","messagePattern":"Entity join treats can not be aliased","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/from/SqmEntityJoin.java","lineNumber":206,"sourceCode":"\t\treturn treatAs( nodeBuilder().getDomainModel().entity( treatAsType ) );\n\t}\n\n\t@Nonnull\n\t@Override\n\tpublic <S extends R> SqmTreatedEntityJoin<L,R,S> treatAs(@Nonnull EntityDomainType<S> treatAsType) {\n\t\tfinal var treat = (SqmTreatedEntityJoin<L, R, S>) findTreat( treatAsType, null );\n\t\tif ( treat == null ) {\n\t\t\treturn addTreat( new SqmTreatedEntityJoin<>( this, (SqmEntityDomainType<S>) treatAsType, null ) );\n\t\t}\n\t\telse {\n\t\t\treturn treat;\n\t\t}\n\t}\n\n\t@Override\n\t@Nonnull\n\tpublic <S extends R> SqmTreatedEntityJoin<L,R,S> treatAs(@Nonnull Class<S> treatJavaType, @Nullable String alias) {\n\t\tthrow new UnsupportedOperationException( \"Entity join treats can not be aliased\" );\n\t}\n\n\t@Override\n\t@Nonnull\n\tpublic <S extends R> SqmTreatedEntityJoin<L,R,S> treatAs(@Nonnull EntityDomainType<S> treatTarget, @Nullable String alias) {\n\t\tthrow new UnsupportedOperationException( \"Entity join treats can not be aliased\" );\n\t}\n\n\t@Override\n\t@Nonnull\n\tpublic <S extends R> SqmTreatedEntityJoin<L,R,S> treatAs(@Nonnull Class<S> treatJavaType, @Nullable String alias, boolean fetched) {\n\t\tthrow new UnsupportedOperationException( \"Entity join treats can not be aliased\" );\n\t}\n\n\t@Override\n\t@Nonnull\n\tpublic <S extends R> SqmTreatedEntityJoin<L,R,S> treatAs(@Nonnull EntityDomainType<S> treatTarget, @Nullable String alias, boolean fetched) {\n\t\tthrow new UnsupportedOperationException( \"Entity join treats can not be aliased\" );","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/from/SqmEntityJoin.java#L188-L224","documentation":"SqmEntityJoin models a join to an unrelated entity type: HQL `join Employee e on ...`, criteria `JpaFrom.join(Class)`/`join(EntityDomainType)`. Treat on an entity join IS supported, but only without its own alias — the working overloads create an SqmTreatedEntityJoin that reuses the original join's identification variable. This treatAs(Class<S> treatJavaType, String alias) overload throws UnsupportedOperationException because the SQM model cannot attach a second alias to a treated entity join.","triggerScenarios":"Calling treatAs(SubType.class, \"t\") on a JpaEntityJoin obtained from root.join(Other.class); HQL that assigns the treat a new alias over an entity join (`join treat(e as Manager) m`); SQM rewriters replaying aliased treats on SqmEntityJoin nodes.","commonSituations":"Copy-pasting treat syntax from attribute joins (where `join treat(o.x as Sub) alias` is legal) onto entity joins; query-DSL helpers that always pass an alias to treatAs; switching a query from an association join to an unrelated entity join while keeping the treated alias.","solutions":["Drop the alias: call the no-alias overload treatAs(SubType.class) — supported for entity joins; the treated node shares the original join alias.","In HQL, use treat as a path expression (`treat(e as Manager).salary`) instead of binding the treat to a new alias.","If a distinct subtype identification variable is truly required, join the subtype entity directly (`join Manager m on ...`) instead of treating."],"exampleFix":"// before - UnsupportedOperationException: Entity join treats can not be aliased\nJpaEntityJoin<Order, Person> ej = order.join( Person.class );\nej.treatAs( Manager.class, \"m\" );\n\n// after - no-alias overload; treated join reuses alias of ej\nJpaTreatedJoin<Order, Person, Manager> treated = ej.treatAs( Manager.class );\nquery.where( cb.gt( treated.get( \"salary\" ), 1000 ) );","handlingStrategy":"type-guard","validationCode":"import org.hibernate.query.sqm.tree.spi.from.*;\n\nif ( join instanceof SqmEntityJoin<?, ?> ) {\n    // entity joins support treat ONLY without an alias\n    treated = join.treatAs( Sub.class );\n} else {\n    treated = join.treatAs( Sub.class, alias );\n}","typeGuard":"static boolean treatSupportsAlias(Join<?, ?> join) {\n    return !( join instanceof SqmEntityJoin<?, ?> );\n}","tryCatchPattern":"try {\n    return join.treatAs( type, alias );\n} catch ( UnsupportedOperationException e ) {\n    if ( \"Entity join treats can not be aliased\".equals( e.getMessage() ) ) {\n        return join.treatAs( type ); // recover once via the no-alias overload\n    }\n    throw e;\n}","preventionTips":["For entity joins, always call the no-alias treatAs(Class) overload; the treat shares the original join alias.","In HQL, write treat as a path expression (treat(e as Manager).salary) instead of binding a new alias.","Need a distinct subtype variable? Join the subtype entity directly rather than treating.","Centralize treats in one helper that knows the entity-join special case."],"tags":["hibernate","hql","jpa","criteria-api","entity-join","treat-as","alias"],"backgroundTag":"unsupported-treat-as","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}