hibernate/hibernate-orm · error · SemanticException
A 'limit', 'offset', or 'fetch' clause requires an 'order by
Error message
A 'limit', 'offset', or 'fetch' clause requires an 'order by' clause when used in a subquery
What it means
Error "A 'limit', 'offset', or 'fetch' clause requires an 'order by' clause when used in a subquery" thrown in hibernate/hibernate-orm.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java:1140
case EXCEPT -> all ? SetOperator.EXCEPT_ALL : SetOperator.EXCEPT;
default -> throw new ParsingException( "Unrecognized set operator: " + token.getText() );
};
}
protected void visitLimitOffset(SqmQueryPart<?> sqmQueryPart, HqlParser.LimitOffsetContext ctx) {
if ( ctx != null ) {
final var limitClauseContext = ctx.limitClause();
final var offsetClauseContext = ctx.offsetClause();
final var fetchClauseContext = ctx.fetchClause();
if ( limitClauseContext != null || offsetClauseContext != null || fetchClauseContext != null ) {
if ( getCreationOptions().useStrictJpaCompliance() ) {
throw new StrictJpaComplianceViolation(
StrictJpaComplianceViolation.Type.LIMIT_OFFSET_CLAUSE
);
}
if ( processingStateStack.depth() > 1 && sqmQueryPart.getOrderByClause() == null ) {
throw new SemanticException(
"A 'limit', 'offset', or 'fetch' clause requires an 'order by' clause when used in a subquery",
query
);
}
setOffsetFetchLimit( sqmQueryPart, limitClauseContext, offsetClauseContext, fetchClauseContext );
}
}
}
protected void visitOrderBy(SqmQueryPart<?> sqmQueryPart, HqlParser.OrderByClauseContext ctx) {
if ( ctx != null ) {
if ( creationOptions.useStrictJpaCompliance() && processingStateStack.depth() > 1 ) {
throw new StrictJpaComplianceViolation(
StrictJpaComplianceViolation.Type.SUBQUERY_ORDER_BY
);
}
sqmQueryPart.setOrderByClause( visitOrderByClause( ctx ) );View on GitHub (pinned to fad1729dce)
Solutions
- Add an 'order by' clause to the subquery using limit/offset/fetch.
- Move the limiting clause to the outer query.
When it happens
Trigger: The HQL/JPQL query violates a structural language rule.
Common situations: Writing queries with missing select/from, mismatched group by positions, or invalid ordering constructs.
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/a8842fb27ecb7ebc.
Report an issue: GitHub.