{"record":{"id":"a8842fb27ecb7ebc","repo":"hibernate/hibernate-orm","slug":"a-limit-offset-or-fetch-clause-requires-an","errorCode":null,"errorMessage":"A 'limit', 'offset', or 'fetch' clause requires an 'order by' clause when used in a subquery","messagePattern":"A 'limit', 'offset', or 'fetch' clause requires an 'order by' clause when used in a subquery","errorType":"exception","errorClass":"SemanticException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java","lineNumber":1140,"sourceCode":"\t\t\tcase EXCEPT -> all ? SetOperator.EXCEPT_ALL : SetOperator.EXCEPT;\n\t\t\tdefault -> throw new ParsingException( \"Unrecognized set operator: \" + token.getText() );\n\t\t};\n\t}\n\n\tprotected void visitLimitOffset(SqmQueryPart<?> sqmQueryPart, HqlParser.LimitOffsetContext ctx) {\n\t\tif ( ctx != null ) {\n\t\t\tfinal var limitClauseContext = ctx.limitClause();\n\t\t\tfinal var offsetClauseContext = ctx.offsetClause();\n\t\t\tfinal var fetchClauseContext = ctx.fetchClause();\n\t\t\tif ( limitClauseContext != null || offsetClauseContext != null || fetchClauseContext != null ) {\n\t\t\t\tif ( getCreationOptions().useStrictJpaCompliance() ) {\n\t\t\t\t\tthrow new StrictJpaComplianceViolation(\n\t\t\t\t\t\t\tStrictJpaComplianceViolation.Type.LIMIT_OFFSET_CLAUSE\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tif ( processingStateStack.depth() > 1 && sqmQueryPart.getOrderByClause() == null ) {\n\t\t\t\t\tthrow new SemanticException(\n\t\t\t\t\t\t\t\"A 'limit', 'offset', or 'fetch' clause requires an 'order by' clause when used in a subquery\",\n\t\t\t\t\t\t\tquery\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tsetOffsetFetchLimit( sqmQueryPart, limitClauseContext, offsetClauseContext, fetchClauseContext );\n\t\t\t}\n\t\t}\n\t}\n\n\tprotected void visitOrderBy(SqmQueryPart<?> sqmQueryPart, HqlParser.OrderByClauseContext ctx) {\n\t\tif ( ctx != null ) {\n\t\t\tif ( creationOptions.useStrictJpaCompliance() && processingStateStack.depth() > 1 ) {\n\t\t\t\tthrow new StrictJpaComplianceViolation(\n\t\t\t\t\t\tStrictJpaComplianceViolation.Type.SUBQUERY_ORDER_BY\n\t\t\t\t);\n\t\t\t}\n\t\t\tsqmQueryPart.setOrderByClause( visitOrderByClause( ctx ) );","sourceCodeStart":1122,"sourceCodeEnd":1158,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java#L1122-L1158","documentation":"Thrown by SemanticQueryBuilder.visitLimitOffset when a subquery (query depth > 1) declares a limit/offset/fetch clause but no 'order by' clause. Without an ordering, pagination of a subquery is non-deterministic, so Hibernate requires an explicit 'order by' before a subquery can be paginated; the offending input is the unpaginatable subquery.","triggerScenarios":"The HQL/JPQL query violates a structural language rule.","commonSituations":"Writing queries with missing select/from, mismatched group by positions, or invalid ordering constructs.","solutions":["Add an 'order by' clause to the subquery using limit/offset/fetch.","Move the limiting clause to the outer query."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}