{"record":{"id":"bc9a15208e4d5b7e","repo":"hibernate/hibernate-orm","slug":"function-root-does-not-have-an-entity-type-use-ge","errorCode":null,"errorMessage":"Function root does not have an entity type. Use getReferencedPathSource() instead.","messagePattern":"Function root does not have an entity type\\. Use getReferencedPathSource\\(\\) instead\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/domain/SqmFunctionRoot.java","lineNumber":99,"sourceCode":"\t\tfinal SqmPathSource<?> pathSource =\n\t\t\t\tfunction.getType().getSubPathSource( CollectionPart.Nature.INDEX.getName() );\n\t\t//noinspection unchecked\n\t\tfinal SqmPathSource<Long> indexPathSource = (SqmPathSource<Long>) pathSource;\n\t\treturn resolvePath( indexPathSource.getPathName(), indexPathSource );\n\t}\n\n\t@Override\n\tpublic <X> X accept(SemanticQueryWalker<X> walker) {\n\t\treturn walker.visitRootFunction( this );\n\t}\n\n\t// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\t// JPA\n\n\t@Nonnull\n\t@Override\n\tpublic SqmEntityDomainType<E> getModel() {\n\t\tthrow new UnsupportedOperationException( \"Function root does not have an entity type. Use getReferencedPathSource() instead.\" );\n\t}\n\n\t@Override\n\tpublic String getEntityName() {\n\t\tthrow new UnsupportedOperationException( \"Function root does not have an entity type. Use getReferencedPathSource() instead.\" );\n\t}\n\n\t@Override\n\tpublic SqmPathSource<E> getResolvedModel() {\n\t\treturn getReferencedPathSource();\n\t}\n\n\t@Override\n\t@Nonnull\n\tpublic SqmCorrelatedRoot<E> createCorrelation() {\n\t\tthrow new UnsupportedOperationException();\n\t}\n","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/domain/SqmFunctionRoot.java#L81-L117","documentation":"SqmFunctionRoot is a query root derived from a function call (Hibernate 7 function roots / set-returning functions in the FROM clause). It has no entity metamodel identity, so the JPA Root.getModel() contract, which promises an SqmEntityDomainType, throws UnsupportedOperationException; the message points you to getReferencedPathSource() (also exposed as getResolvedModel()) for the actual path source.","triggerScenarios":"Calling getModel() on a root created for a function in the FROM clause, e.g. 'from unnest(...) u' / json_table roots, through generic criteria-inspection code, query transformers, or libraries that assume every SqmRoot is entity-backed.","commonSituations":"Spring Data JPA / Blaze-Persistence / reporting tooling calling getModel() or getEntityName() on arbitrary roots; custom query copy/rewrite utilities iterating over query roots; adoption of Hibernate 7 function roots in codebases with generic criteria infrastructure.","solutions":["Replace getModel() with getResolvedModel()/getReferencedPathSource(), which works for both entity and function roots","Guard generic code: if (root instanceof SqmFunctionRoot) skip entity-specific handling such as getModel()/getEntityName()","If you truly need the entity type, obtain it from the function's return type (referenced path source's path type) instead"],"exampleFix":"// before\nSqmRoot<?> root = ...; // may be a function root\nString name = root.getModel().getHibernateEntityName(); // throws on SqmFunctionRoot\n\n// after\nif (root instanceof SqmFunctionRoot<?> fr) {\n    SqmPathSource<?> src = fr.getReferencedPathSource(); // no entity metamodel exists\n} else {\n    String name2 = root.getModel().getHibernateEntityName();\n}","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static SqmPathSource<?> referencedSource(SqmRoot<?> root) {\n    // works for entity roots and function roots alike\n    return root instanceof SqmFunctionRoot<?> fr\n        ? fr.getReferencedPathSource()\n        : root.getModel();\n}","tryCatchPattern":"try {\n    SqmEntityDomainType<?> t = root.getModel();\n} catch (UnsupportedOperationException e) {\n    // function root has no entity type: fall back to getReferencedPathSource()\n    SqmPathSource<?> src = ((SqmFunctionRoot<?>) root).getReferencedPathSource();\n}","preventionTips":["Prefer getResolvedModel()/getReferencedPathSource() over getModel() in generic root handling","Branch on 'root instanceof SqmFunctionRoot' before JPA Root contract calls","Do not assume every SqmRoot has an entity name in Hibernate 7 codebases using function roots","Cover function-root queries in tests for custom criteria utilities"],"tags":["hibernate","criteria-api","function-root","jpa-api","sqm"],"backgroundTag":"unsupported-jpa-operation","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}