{"record":{"id":"80c7d598b7ab7954","repo":"hibernate/hibernate-orm","slug":"unmapped-polymorphism","errorCode":"UNMAPPED_POLYMORPHISM","errorMessage":"Encountered unmapped polymorphic reference [${entityName}], but strict JPQL compliance was requested","messagePattern":"Encountered unmapped polymorphic reference \\[(.+?)\\], but strict JPQL compliance was requested","errorType":"exception","errorClass":"StrictJpaComplianceViolation","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java","lineNumber":2121,"sourceCode":"\t@Override\n\tpublic SqmRoot<?> visitRootEntity(HqlParser.RootEntityContext ctx) {\n\t\tfinal var entityNameContext = ctx.entityName();\n\t\tfinal String name = getEntityName( entityNameContext );\n\n\t\tfinal var entityDescriptor = creationContext.getJpaMetamodel().getHqlEntityReference( name );\n\n\t\tfinal String alias = extractAlias( ctx.variable() );\n\n\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;","sourceCodeStart":2103,"sourceCodeEnd":2139,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java#L2103-L2139","documentation":"Raised in root resolution: the from-clause entity name resolved to an unmapped polymorphic reference (SqmPolymorphicRootDescriptor) while strict JPQL query compliance is enabled, so Hibernate refuses to run the Hibernate-specific polymorphic query (StrictJpaComplianceViolation.Type.UNMAPPED_POLYMORPHISM). The message reports the Hibernate entity name of the polymorphic descriptor.","triggerScenarios":"'from java.lang.Object o' or a package-namespace polymorphic root (e.g. 'from com.acme.domain') with 'hibernate.jpa.compliance.query=true' (or global 'hibernate.jpa.compliance=true').","commonSituations":"Enabling JPA compliance globally for certification/consistency while existing code uses Hibernate's polymorphic HQL feature; Spring Boot configs inheriting compliance settings from a shared baseline.","solutions":["Replace the polymorphic name with a concrete entity name","Set 'hibernate.jpa.compliance.query=false' to re-enable the Hibernate extension","Scope compliance to the queries that need it instead of the whole SessionFactory"],"exampleFix":"// before\nList<Object> rows = em.createQuery(\"from java.lang.Object o\", Object.class).getResultList();  // with compliance on\n\n// after\nList<Employee> rows = em.createQuery(\"from Employee e\", Employee.class).getResultList();","handlingStrategy":"fallback","validationCode":"static boolean polymorphicRoot(EntityManagerFactory emf, String name) {\n    // a name that resolves to zero or many entities behaves polymorphically in HQL\n    long n = emf.getMetamodel().getEntities().stream().filter(e -> e.getName().equals(name)).count();\n    return n == 0 && name.contains(\".\"); // namespace/object style names\n}\n\nif (polymorphicRoot(emf, name) && strictQueryCompliance(emf)) {\n    throw new IllegalArgumentException(\"Polymorphic root not allowed under strict JPA compliance: \" + name);\n}","typeGuard":"static boolean isStrictJpaViolation(Throwable t) {\n    return t instanceof org.hibernate.query.sqm.StrictJpaComplianceViolation;\n}","tryCatchPattern":"try {\n    return em.createQuery(\"from \" + name + \" o\", Object.class).getResultList();\n} catch (org.hibernate.query.sqm.StrictJpaComplianceViolation e) {\n    return em.createQuery(\"from \" + concreteEntityName + \" o\", Object.class).getResultList(); // concrete fallback\n}","preventionTips":["Avoid polymorphic from-roots in code shared across compliance settings","Pin 'hibernate.jpa.compliance.query=false' in properties if you rely on Hibernate polymorphism","Add a startup assertion listing queries that use namespace-style entity names"],"tags":["hql","jpa-compliance","polymorphism","hibernate","from-clause"],"backgroundTag":"strict-jpa-compliance-violation","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}