{"record":{"id":"702ccfdc25b375f0","repo":"hibernate/hibernate-orm","slug":"cannot-apply-treat-operator-to-discriminator-path","errorCode":null,"errorMessage":"Cannot apply TREAT operator to discriminator path","messagePattern":"Cannot apply TREAT operator to discriminator path","errorType":"exception","errorClass":"UnsupportedMappingException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/spi/DiscriminatorSqmPath.java","lineNumber":112,"sourceCode":"\t@Nonnull\n\t@Override\n\tdefault <N extends Number & Comparable<N>> NumericExpression<N> get(@Nonnull NumericAttribute<? super T, N> attribute) {\n\t\tthrow new IllegalStateException( \"Discriminator cannot be de-referenced\" );\n\t}\n\n\t@Nonnull\n\t@Override\n\tdefault TextExpression get(@Nonnull TextAttribute<? super T> attribute) {\n\t\tthrow new IllegalStateException( \"Discriminator cannot be de-referenced\" );\n\t}\n\n\n\n\t@Nonnull\n\t@SuppressWarnings({ \"rawtypes\", \"unchecked\" })\n\t@Override\n\tdefault SqmTreatedPath treatAs(@Nonnull Class treatJavaType) {\n\t\tthrow new UnsupportedMappingException( \"Cannot apply TREAT operator to discriminator path\" );\n\t}\n\n\t@Nonnull\n\t@SuppressWarnings({ \"rawtypes\", \"unchecked\" })\n\t@Override\n\tdefault SqmTreatedPath treatAs(@Nonnull EntityDomainType treatTarget) {\n\t\tthrow new UnsupportedMappingException( \"Cannot apply TREAT operator to discriminator path\" );\n\t}\n\n\t@SuppressWarnings({ \"rawtypes\", \"unchecked\" })\n\t@Override\n\t@Nonnull\n\tdefault SqmTreatedPath treatAs(@Nonnull Class treatJavaType, @Nullable String alias) {\n\t\tthrow new UnsupportedMappingException( \"Cannot apply TREAT operator to discriminator path\" );\n\t}\n\n\t@SuppressWarnings({ \"rawtypes\", \"unchecked\" })\n\t@Override","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/spi/DiscriminatorSqmPath.java#L94-L130","documentation":"TREAT (...) narrows an entity path to a subtype; applying it to a discriminator path (the result of TYPE(p)) is meaningless because the discriminator already is the subtype marker, not an entity reference. DiscriminatorSqmPath.treatAs(Class) therefore throws UnsupportedMappingException (a HibernateException, non-transient) to fail fast on such queries.","triggerScenarios":"HQL 'TREAT(type(p) AS Employee)' or Criteria 'root.type().treatAs(Employee.class)'. Also generic code that calls treatAs(...) on every SqmPath it visits while walking a query tree that contains a type() expression.","commonSituations":"Query-rewriting utilities (security filters, tenant filters, pagination wrappers) that inject treatAs calls into paths; teams migrating legacy HQL that mixed TYPE() and TREAT syntax after a Hibernate 6 upgrade; LLM- or template-generated HQL that nests treat inside type().","solutions":["Apply TREAT to the entity path, not the discriminator: 'TREAT(p AS Employee)' / root.treatAs(Employee.class)","Drop the nested treat entirely and query the subtype: 'from Employee e'","If you meant to filter by subtype, use the discriminator comparison instead: where type(p) = Employee","Audit generated/templated HQL for 'type(' followed by 'treat(' or '.treatAs(' on type() nodes"],"exampleFix":"-- before (HQL)\nselect treat(type(p) as Employee).salary from Person p\n-- after\nselect treat(p as Employee).salary from Person p","handlingStrategy":"type-guard","validationCode":"if (path instanceof DiscriminatorSqmPath) { /* skip treat, use type() = Class predicate instead */ } else { path.treatAs(Employee.class); }","typeGuard":"static boolean treatable(SqmPath<?> p) { return !(p instanceof org.hibernate.query.sqm.spi.DiscriminatorSqmPath); }","tryCatchPattern":"catch (UnsupportedMappingException e) { /* query rejected: apply treat to the entity root instead */ }","preventionTips":["Apply TREAT only to entity aliases in HQL","Static-search code for 'type().treatAs' and 'treat(type('","Keep generated-HQL templates under test"],"tags":["hibernate","hql","treat","discriminator","sqm","polymorphism"],"backgroundTag":"treat-operator-misuse","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}