{"record":{"id":"9caac283e0865302","repo":"hibernate/hibernate-orm","slug":"implicitly-polymorphic-domain-path-in-subquery","errorCode":null,"errorMessage":"Implicitly-polymorphic domain path in subquery '${name}'","messagePattern":"Implicitly-polymorphic domain path in subquery '(.+?)'","errorType":"exception","errorClass":"SemanticException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java","lineNumber":2130,"sourceCode":"\t\tfinal var processingState = processingStateStack.getCurrent();\n\t\tfinal var pathRegistry = processingState.getPathRegistry();\n\t\tif ( entityDescriptor == null ) {\n\t\t\treturn resolveRootEntity( entityNameContext, name, alias, processingState, pathRegistry );\n\t\t}\n\t\tcheckFQNEntityNameJpaComplianceViolationIfNeeded( name, entityDescriptor );\n\n\t\tif ( entityDescriptor instanceof SqmPolymorphicRootDescriptor ) {\n\t\t\tif ( getCreationOptions().useStrictJpaCompliance() ) {\n\t\t\t\tthrow new StrictJpaComplianceViolation(\n\t\t\t\t\t\t\"Encountered unmapped polymorphic reference [\"\n\t\t\t\t\t\t\t\t+ entityDescriptor.getHibernateEntityName()\n\t\t\t\t\t\t\t\t+ \"], but strict JPQL compliance was requested\",\n\t\t\t\t\t\tStrictJpaComplianceViolation.Type.UNMAPPED_POLYMORPHISM\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tif ( processingStateStack.depth() > 1 ) {\n\t\t\t\tthrow new SemanticException(\n\t\t\t\t\t\t\"Implicitly-polymorphic domain path in subquery '\" + entityDescriptor.getName() + \"'\",\n\t\t\t\t\t\tquery\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\tfinal var sqmRoot = new SqmRoot<>( entityDescriptor, alias, true, nodeBuilder() );\n\t\tpathRegistry.register( sqmRoot );\n\t\treturn sqmRoot;\n\t}\n\n\tprivate SqmRoot<?> resolveRootEntity(\n\t\t\tHqlParser.EntityNameContext entityNameContext,\n\t\t\tString name, String alias,\n\t\t\tSqmCreationProcessingState processingState,\n\t\t\tSqmPathRegistry pathRegistry) {\n\t\tfinal var entityNameParseTreeChildren = entityNameContext.children;\n\t\tfinal int size = entityNameParseTreeChildren.size();","sourceCodeStart":2112,"sourceCodeEnd":2148,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java#L2112-L2148","documentation":"A polymorphic root (SqmPolymorphicRootDescriptor) was used as the root of a subquery (processingStateStack.depth() > 1). Implicit polymorphism is only supported for top-level roots; Hibernate cannot enumerate the matching entity set for a nested domain path, so it throws SemanticException naming the polymorphic descriptor.","triggerScenarios":"'select e from Employee e where exists (select 1 from java.lang.Object o where ...)'; any subquery whose from-clause root is a namespace/polymorphic name.","commonSituations":"Reusing a working polymorphic top-level query as an exists/in-subquery; generic framework code that builds subqueries from user-supplied entity names.","solutions":["Use a concrete entity name as the subquery root","Move the polymorphic part to the outer query and correlate the concrete subquery","Restructure the predicate so no polymorphic root is needed (e.g. member-of checks on associations)"],"exampleFix":"// before\nselect e from Employee e where exists (select 1 from java.lang.Object o where ...)\n\n// after\nselect e from Employee e where exists (select 1 from Project p where p.lead = e)","handlingStrategy":"validation","validationCode":"// Do not allow polymorphic/namespace names as subquery roots in generated HQL\nstatic boolean subqueryRootIsConcrete(EntityManagerFactory emf, String rootName) {\n    return emf.getMetamodel().getEntities().stream().anyMatch(e -> e.getName().equals(rootName));\n}\nif (!subqueryRootIsConcrete(emf, subqueryRoot)) throw new IllegalArgumentException(\"Use a concrete entity as subquery root: \" + subqueryRoot);","typeGuard":null,"tryCatchPattern":"try {\n    return em.createQuery(hql, Employee.class).getResultList();\n} catch (org.hibernate.query.sqm.SemanticException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"Implicitly-polymorphic domain path in subquery\")) {\n        throw new IllegalArgumentException(\"Replace the polymorphic subquery root with a concrete entity\", e);\n    }\n    throw e;\n}","preventionTips":["Always use a concrete entity name for subquery from-roots","If polymorphic results are needed, keep polymorphism at the outermost query level","Unit-test generic query builders with namespace names to catch this early"],"tags":["hql","polymorphism","subquery","hibernate"],"backgroundTag":"polymorphic-query-unsupported","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}