Repository navigation
Order by a @OneToMany or @ElementCollection field via a subquery - #71
Merged
Merged
Conversation
Ordering on a field of a @onetomany was left out of the SQL whenever PostponedFetchRoot was used, and PostponedFetchRoot sorted only the entities of the current page in memory. Ordering on a field of an @ElementCollection joined the collection, which returned an entity once per element, so the same entity could show up on two pages. The entities are now ordered by the smallest value of the field among the elements when ascending, or else by the largest one, via a correlated subquery which also applies the required criteria on the collection. The ID is added as tiebreaker. The subquery is wrapped in COALESCE, as EclipseLink does not accept a subquery as order expression. In a query which is distinct because it joins a collection, the order is skipped with a warning, unless the collection is fetched. Value based paging does not apply to such an ordering. The query is no longer distinct merely because it has optional criteria, only when it joins a collection. Fixes #61 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #61
Ordering on a field of a
@OneToManywas left out of the SQL wheneverPostponedFetchRootwas used, andPostponedFetchRootsorted only the entities of the current page in memory. Ordering on a field of an@ElementCollectionjoined the collection, which returned an entity once per element, so the same entity could show up on two pages.BaseEntityService#buildOrderBy()now orders the entities by the smallest value of the field among the elements when ascending, or else by the largest one:COALESCEwith a typedNULLfallback. EclipseLink's criteria API casts every order expression toExpressionImpl, which a subquery is not. SQL Server does not acceptCOALESCEwith only one argument.Pagejavadoc says so.PostponedFetchRootno longer re-sorts the entities in memory. It still sorts the elements of the fetched collections.New tests in
OmniPersistenceITread two pages and assert the order across them:@OneToManyascending and descending with and without fetch, with required criteria, with a list and a negated criteria, with optional criteria, an@ElementCollection, and an@ElementCollectionnested in a@OneToMany.Known limitations:
LIMIT, which costs more than a plain order on a large table.🤖 Generated with Claude Opus 5.5