{"record":{"id":"7ad0fd6b176456fd","repo":"hibernate/hibernate-orm","slug":"embeddable-paths-cannot-be-treat-ed-to-an-entity-t","errorCode":null,"errorMessage":"Embeddable paths cannot be TREAT-ed to an entity type","messagePattern":"Embeddable paths cannot be TREAT-ed to an entity type","errorType":"exception","errorClass":"TreatException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/domain/SqmEmbeddedValuedSimplePath.java","lineNumber":109,"sourceCode":"\t\treturn sqmPath;\n\t}\n\n\t@Override\n\tpublic <X> X accept(SemanticQueryWalker<X> walker) {\n\t\treturn walker.visitEmbeddableValuedPath( this );\n\t}\n\n\n\t@Nonnull\n\t@Override\n\tpublic <S extends T> SqmTreatedPath<T, S> treatAs(@Nonnull Class<S> treatJavaType) {\n\t\treturn getTreatedPath( nodeBuilder().getDomainModel().embeddable( treatJavaType ) );\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 to an entity type\" );\n\t}\n\n\t@Override\n\tpublic JavaType<T> getExpressibleJavaType() {\n\t\treturn super.getExpressible().getExpressibleJavaType();\n\t}\n\n\t@Override\n\tpublic @Nonnull Class<T> getJavaType() {\n\t\treturn getJavaTypeDescriptor().getJavaTypeClass();\n\t}\n\n\t@Override\n\tpublic JavaType<?> getRelationalJavaType() {\n\t\treturn super.getExpressible().getRelationalJavaType();\n\t}\n}\n","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/domain/SqmEmbeddedValuedSimplePath.java#L91-L127","documentation":"Thrown by Hibernate's SQM tree when TREAT is applied to an embeddable-valued path with an entity type as the target. JPA TREAT is a downcast within an entity inheritance hierarchy; embeddables have no identity and no inheritance, so SqmEmbeddedValuedSimplePath.treatAs(EntityDomainType) unconditionally throws TreatException (a HibernateException). Note that the Class-based overload only accepts another embeddable type resolved from the domain model.","triggerScenarios":"Calling path.treatAs(SomeEntity.class) or path.treatAs(entityDomainType) on a path whose referenced source is an @Embedded/@EmbeddedId attribute, e.g. root.get(\"address\").treatAs(AddressEntity.class) in criteria, or HQL like 'join treat(p.address as USAddress) a' where address is @Embedded and USAddress is an @Entity.","commonSituations":"Modeling polymorphic embedded data with entity classes and then trying to TREAT it; confusing an @Embedded attribute with a @ManyToOne association when porting queries; users discovering Hibernate 7's embeddable-treat support and passing an entity class as target; migrating native SQL CASE-on-type idioms into HQL TREAT.","solutions":["If the attribute is @Embedded and you need polymorphism, remodel it as a @ManyToOne/@OneToOne entity association and apply TREAT to that association path","If you intended Hibernate 7 embeddable treat, make sure the target is an embeddable type registered in the domain model (treatAs(Class) resolves it via embeddable(treatJavaType)), not an entity","If no polymorphism is needed, drop the TREAT and navigate the embeddable's sub-attributes directly (p.address.zip)","Check the mapping annotations: @Embedded vs @ManyToOne on the referenced attribute decides whether TREAT can ever apply"],"exampleFix":"// before: address is @Embeddable, USAddress is an @Entity\n// HQL: select a.zip from Person p join treat(p.address as USAddress) a\n//  -> TreatException: Embeddable paths cannot be TREAT-ed to an entity type\n\n// after: model it as an entity association with inheritance\n// @Entity class Address { ... }\n// @Entity class USAddress extends Address { ... }\n// @ManyToOne Address address;\n// HQL: select a.zip from Person p join treat(p.address as USAddress) a","handlingStrategy":"validation","validationCode":"// before calling treatAs on a path, check its referenced type\nif (path.getReferencedPathSource().getPathType() instanceof EmbeddableDomainType<?>) {\n    // treatAs(EntityDomainType) would throw TreatException:\n    // either navigate sub-attributes or remodel as an entity association\n    return path.get(subAttributeName);\n}\nreturn path.treatAs(targetEntity);","typeGuard":"static boolean isEntityValuedPath(SqmPath<?> path) {\n    return path.getReferencedPathSource().getPathType() instanceof IdentifiableDomainType<?>;\n}","tryCatchPattern":"try {\n    treated = path.treatAs(target);\n} catch (TreatException e) {\n    // path is embeddable/basic: report a query-model error, do not retry with the same target\n    throw new IllegalArgumentException(\"TREAT unsupported on \" + path.getNavigablePath(), e);\n}","preventionTips":["Only apply TREAT to paths that navigate a @ManyToOne/@OneToOne to an entity in an inheritance hierarchy","Check the attribute's annotation (@Embedded vs @ManyToOne) before writing TREAT queries against it","Treat targets must be mapped subtypes in the same hierarchy as the path's entity type","In generic criteria code, branch on getReferencedPathSource().getPathType() before calling treatAs"],"tags":["hibernate","hql","criteria-api","treat","embeddable","sqm"],"backgroundTag":"invalid-treat-target","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}