{"record":{"id":"d759462d007f6151","repo":"hibernate/hibernate-orm","slug":"the-limit-and-fetch-clauses-may-not-be-used-to","errorCode":null,"errorMessage":"The 'limit' and 'fetch' clauses may not be used together","messagePattern":"The 'limit' and 'fetch' clauses may not be used together","errorType":"exception","errorClass":"SemanticException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java","lineNumber":1176,"sourceCode":"\t\t\tsqmQueryPart.setOrderByClause( visitOrderByClause( ctx ) );\n\t\t}\n\t}\n\n\t@SuppressWarnings(\"unchecked\")\n\tprivate void setOffsetFetchLimit(SqmQueryPart<?> sqmQueryPart, HqlParser.LimitClauseContext limitClauseContext, HqlParser.OffsetClauseContext offsetClauseContext, HqlParser.FetchClauseContext fetchClauseContext) {\n\t\t// these casts are all fine because the parser only accepts literals and parameters\n\t\tsqmQueryPart.setOffsetExpression( (SqmExpression<? extends Number>) visitOffsetClause(offsetClauseContext) );\n\t\tif ( limitClauseContext == null ) {\n\t\t\tsqmQueryPart.setFetchExpression(\n\t\t\t\t\t(SqmExpression<? extends Number>) visitFetchClause(fetchClauseContext),\n\t\t\t\t\tvisitFetchClauseType(fetchClauseContext)\n\t\t\t);\n\t\t}\n\t\telse if ( fetchClauseContext == null ) {\n\t\t\tsqmQueryPart.setFetchExpression( (SqmExpression<? extends Number>) visitLimitClause(limitClauseContext) );\n\t\t}\n\t\telse {\n\t\t\tthrow new SemanticException(\"The 'limit' and 'fetch' clauses may not be used together\", query );\n\t\t}\n\t}\n\n\t@Override\n\tpublic SqmQuerySpec<?> visitQuery(HqlParser.QueryContext ctx) {\n\t\tfinal var sqmQuerySpec = currentQuerySpec();\n\n\t\tfinal var fromClauseContext = ctx.fromClause();\n\t\tfinal var whereClauseContext = ctx.whereClause();\n\t\tfinal var groupByClauseContext = ctx.groupByClause();\n\t\tfinal var havingClauseContext = ctx.havingClause();\n\t\tfinal var selectClauseContext = ctx.selectClause();\n\n\t\tif ( havingClauseContext != null && groupByClauseContext == null ) {\n\t\t\tthrow new SemanticException( \"Query has 'having' but no 'group by'\", query );\n\t\t}\n\n\t\t// visit from clause first!!!","sourceCodeStart":1158,"sourceCodeEnd":1194,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java#L1158-L1194","documentation":"Error \"The 'limit' and 'fetch' clauses may not be used together\" thrown in hibernate/hibernate-orm.","triggerScenarios":"A runtime precondition of the Hibernate API is violated.","commonSituations":"Occurs when application code or configuration does not satisfy the documented contract of the API being called.","solutions":["Use either 'limit' or 'fetch first', not both, in the query.","Remove one of the limiting clauses."],"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"}