JPA OrderBy annotated relation not ordered when using entity graph with criteria api

Description

We have a unique use case that requires combining multiple features to achieve the expected results. Our issue arose after upgrading from Hibernate 6.5 to 6.6.6.

Issue:

When fetching a list of entities with a one-to-many mapped relationship that should be ordered using JPA’s @OrderByannotation, the generated SQL SELECT query does not include the ordering clause, resulting in an unordered list.

Steps to Reproduce:

This unexpected behavior occurs when all of the following conditions are met:

  • A mapped one-to-many relationship is annotated with @OrderBy.

  • An entity graph is used in fetchgraph mode.

  • The Criteria API is used to dynamically construct the query.

  • A JOIN is explicitly defined within the Criteria query for the same relationship that is already included in the entity graph.

  • The defined JOIN can be omitted from any applied filter conditions.

I have created test cases to reproduce this issue and have also proposed a potential solution:

 

The changes included in the solution of can be related to this issue.

This finding also can be related to this issue:

Activity

Show:
Fixed

Details

Assignee

Reporter

Worked in

Components

Fix versions

Affects versions

Priority

Created February 26, 2025 at 10:53 PM
Updated 21 hours ago
Resolved 21 hours ago