{"record":{"id":"ce0342b3b2fd5ea2","repo":"hibernate/hibernate-orm","slug":"embeddable-paths-cannot-be-treat-ed","errorCode":null,"errorMessage":"Embeddable paths cannot be TREAT-ed","messagePattern":"Embeddable paths cannot be TREAT-ed","errorType":"exception","errorClass":"TreatException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/domain/SqmFunctionPath.java","lineNumber":161,"sourceCode":"\t\tfinal SqmFunctionPath<?> path = new SqmFunctionPath<>( result );\n\t\tpathRegistry.register( path );\n\t\treturn path;\n\t}\n\n\t@Override\n\tpublic <X> X accept(SemanticQueryWalker<X> walker) {\n\t\treturn walker.visitFunctionPath( this );\n\t}\n\n\t@Override\n\tpublic void appendHqlString(StringBuilder hql, SqmRenderContext context) {\n\t\tfunction.appendHqlString( hql, context );\n\t}\n\n\t@Nonnull\n\t@Override\n\tpublic <S extends T> SqmTreatedPath<T,S> treatAs(@Nonnull Class<S> treatJavaType) {\n\t\tthrow new TreatException( \"Embeddable paths cannot be TREAT-ed\" );\n\t}\n\n\t@Nonnull\n\t@Override\n\tpublic <S extends T> SqmTreatedPath<T,S> treatAs(@Nonnull EntityDomainType<S> treatTarget) {\n\t\tthrow new TreatException( \"Embeddable paths cannot be TREAT-ed\" );\n\t}\n\n\t@Override\n\tpublic boolean equals(@Nullable Object object) {\n\t\treturn super.equals( object )\n\t\t\t&& function.equals( ((SqmFunctionPath<?>) object).function );\n\t}\n\n\t@Override\n\tpublic int hashCode() {\n\t\tint result = super.hashCode();\n\t\tresult = 31 * result + function.hashCode();","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/domain/SqmFunctionPath.java#L143-L179","documentation":"SqmFunctionPath rejects TREAT with TreatException: when the function result is embeddable-valued, treating it to an entity type is meaningless (embeddables have no inheritance or identity), so treatAs(Class) throws. Unlike SqmEmbeddedValuedSimplePath, the function path has no embeddable-treat delegation at all.","triggerScenarios":"HQL 'treat(f(a) as SomeEntity)' where f returns an embeddable; criteria calling functionPath.treatAs(SomeEntity.class) on a navigable function result over an embeddable type.","commonSituations":"Structured function results (e.g. functions returning a mapped embeddable) that developers try to downcast; mixing up entity and embeddable targets when reusing TREAT snippets; generic query DSLs that attach treatAs to every path.","solutions":["Drop the TREAT and navigate the embeddable's attributes directly from the function result (f(a).subAttr)","If polymorphism is required, make the function return an entity type and use it as a function root instead of a path","Verify the target class: if it is an @Embeddable you may not treat at all on function paths; if it is an @Entity the whole construct is invalid here"],"exampleFix":"// before: address_of() returns an embeddable; USAddress is an @Entity\nselect treat(address_of(p) as USAddress).zip from Person p\n\n// after: navigate the embeddable result directly\nselect address_of(p).zip from Person p","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static boolean supportsTreatToEntity(SqmPath<?> p) {\n    return !(p instanceof SqmFunctionPath<?>)\n        && !(p instanceof SqmFkExpression<?>)\n        && p.getReferencedPathSource().getPathType() instanceof IdentifiableDomainType<?>;\n}","tryCatchPattern":"try {\n    treated = functionPath.treatAs(targetClass);\n} catch (TreatException e) {\n    // embeddable-valued function result: navigate attributes directly instead of treating\n    throw new IllegalStateException(\"Function paths cannot be TREAT-ed; navigate f().attr directly\", e);\n}","preventionTips":["Never TREAT embeddable-valued function results; navigate their attributes directly","If subtype behavior is needed, have the function return an entity type and use a function root","In generic query DSLs, instanceof-check SqmFunctionPath before treatAs"],"tags":["hibernate","hql","treat","custom-function","embeddable","sqm"],"backgroundTag":"invalid-treat-target","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}