{"record":{"id":"286eac06088811bc","repo":"hibernate/hibernate-orm","slug":"cross-join-treats-can-not-be-aliased","errorCode":null,"errorMessage":"Cross join treats can not be aliased","messagePattern":"Cross join treats can not be aliased","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/from/SqmCrossJoin.java","lineNumber":208,"sourceCode":"\t}\n\n\t@Override\n\t@Nonnull\n\tpublic <S extends T> SqmTreatedCrossJoin<L, T, 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 T> SqmTreatedCrossJoin<L, T, S> treatAs(@Nonnull Class<S> treatJavaType, @Nullable String alias, boolean fetch) {\n\t\treturn treatAs( nodeBuilder().getDomainModel().entity( treatJavaType ), alias, false );\n\t}\n\n\t@Override\n\t@Nonnull\n\tpublic <S extends T> SqmTreatedCrossJoin<L, T, S> treatAs(@Nonnull EntityDomainType<S> treatTarget, @Nullable String alias, boolean fetch) {\n\t\tif ( alias != null ) {\n\t\t\tthrow new IllegalArgumentException( \"Cross join treats can not be aliased\" );\n\t\t}\n\t\tif ( fetch ) {\n\t\t\tthrow new IllegalArgumentException( \"Cross join treats can not be fetched\" );\n\t\t}\n\t\tfinal var treat = (SqmTreatedCrossJoin<L, T, S>) findTreat( treatTarget, null );\n\t\tif ( treat == null ) {\n\t\t\treturn addTreat( new SqmTreatedCrossJoin<>( this, (SqmEntityDomainType<S>) treatTarget ) );\n\t\t}\n\t\telse {\n\t\t\treturn treat;\n\t\t}\n\t}\n\n\t@Nonnull\n\t@Override\n\tpublic <S extends T> SqmTreatedCrossJoin<L, T, S> treatAs(@Nonnull Class<S> treatAsType) {\n\t\treturn treatAs( treatAsType, null, false );\n\t}","sourceCodeStart":190,"sourceCodeEnd":226,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/from/SqmCrossJoin.java#L190-L226","documentation":"TREAT AS turns a join into a subtype view, and an alias on a treat creates a new identification variable. SqmCrossJoin.treatAs(EntityDomainType, alias, fetch) throws IllegalArgumentException('Cross join treats can not be aliased') when alias != null, because the underlying cross join has no identification variable to attach the treat alias to. (The Class overload delegates with alias=null, fetch=false.)","triggerScenarios":"Criteria code calling crossJoin.treatAs(Sub.class, \"sub\") or the three-arg overload with a non-null alias; generic join-processing code that uniformly aliases every treatAs call without distinguishing join kinds.","commonSituations":"Reusing treatAs code written for explicit joins on a cross join; porting JPA Criteria treat() usage to Hibernate cross joins; HQL treat(...) with an alias applied to a cross-joined entity.","solutions":["Use the alias-less form - crossJoin.treatAs(Sub.class) - and reference the returned treated node directly in predicates.","If you need an aliased treat, replace the cross join with an explicit join, which supports aliased treats.","In generic code, pass alias=null when the join is an SqmCrossJoin."],"exampleFix":"// before\nSqmTreatedCrossJoin<?, ?, Sub> t = crossJoin.treatAs( Sub.class, \"sub\" ); // IllegalArgumentException\n\n// after\nSqmTreatedCrossJoin<?, ?, Sub> t = crossJoin.treatAs( Sub.class );\ncq.where( cb.equal( t.get( Sub_.code ), \"X\" ) );","handlingStrategy":"validation","validationCode":"static boolean treatSupportsAlias(SqmJoin<?, ?> join) {\n    return !( join instanceof SqmCrossJoin );\n}\n// usage:\njoin.treatAs( Sub.class, treatSupportsAlias( join ) ? \"sub\" : null, false );","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never alias treats on cross joins - use the alias-less treatAs(Class) form.","Branch on join type (SqmCrossJoin vs explicit join) in generic treat code.","Prefer explicit joins when an aliased treat is required."],"tags":["hibernate","sqm","cross-join","treat","alias","criteria-api"],"backgroundTag":"unsupported-treat-operation","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}