{"record":{"id":"702a1dcb1361e0ec","repo":"hibernate/hibernate-orm","slug":"query-group-can-t-be-treated-as-query-spec-use-jp","errorCode":null,"errorMessage":"Query group can't be treated as query spec. Use JpaSelectCriteria#getQueryPart to access query group details","messagePattern":"Query group can't be treated as query spec\\. Use JpaSelectCriteria#getQueryPart to access query group details","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/select/SqmSelectStatement.java","lineNumber":198,"sourceCode":"\t@Nonnull\n\t@Override\n\tpublic List<Order> getOrderList() {\n\t\treturn unmodifiableList( getQueryPart().getSortSpecifications() );\n\t}\n\n\t@Override\n\tpublic SqmQuerySource getQuerySource() {\n\t\treturn querySource;\n\t}\n\n\t@Nonnull\n\t@Override\n\tpublic SqmQuerySpec<T> getQuerySpec() {\n\t\tif ( querySource == CRITERIA ) {\n\t\t\tif ( getQueryPart() instanceof SqmQuerySpec<T> querySpec ) {\n\t\t\t\treturn querySpec;\n\t\t\t}\n\t\t\tthrow new IllegalStateException(\n\t\t\t\t\t\"Query group can't be treated as query spec. Use JpaSelectCriteria#getQueryPart to access query group details\"\n\t\t\t);\n\t\t}\n\t\telse {\n\t\t\treturn super.getQuerySpec();\n\t\t}\n\t}\n\n\tpublic boolean producesUniqueResults() {\n\t\t// For query groups we have to assume that duplicates are possible\n\t\treturn !( getQueryPart() instanceof SqmQuerySpec<?> querySpec )\n\t\t\t|| querySpec.producesUniqueResults();\n\t}\n\n\tpublic boolean containsCollectionFetches() {\n\t\treturn containsCollectionFetches( getQueryPart() );\n\t}\n","sourceCodeStart":180,"sourceCodeEnd":216,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/select/SqmSelectStatement.java#L180-L216","documentation":"Error \"Query group can't be treated as query spec. Use JpaSelectCriteria#getQueryPart to access query group details\" thrown in hibernate/hibernate-orm.","triggerScenarios":"Code accesses a select statement's query spec although the statement is a query group (set operations).","commonSituations":"Calling getQuerySpec() on a UNION query; use JpaSelectCriteria#getQueryPart to inspect the group.","solutions":["Use JpaSelectCriteria#getQueryPart to access the query group instead of calling query-spec methods on it.","Check whether the statement represents a query group (UNION etc.) before calling single-query methods.","If you only need the first query part, navigate through getQueryPart() and cast to the spec you need."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}