Repository navigation
Order by a collection field via an ID query on DB2 - #76
Merged
Merged
Conversation
DB2 does not accept a correlated subquery as order expression (SQLSTATE 42703), so ordering on a field of a @onetomany or @ElementCollection via the COALESCE subquery fails there. On DB2 the page is now loaded in two queries. The first selects the IDs of the page, grouped by ID, ordered by MIN or MAX of a CASE over a LEFT JOIN of the collection, which applies the required criteria on the collection per element. The restrictions are those of the entity query, plus the EXISTS subquery of the count query when a QueryBuilder is used. The second query loads the entities having those IDs, which are then put in the order of the IDs. EclipseLink and OpenJPA do not register parameters which occur only in the ORDER BY clause, so the element criteria also appear in an always true WHERE predicate. OpenJPA cannot render a NULL literal in a CASE, so NULLIF(field, field) is used instead. Follow-up to #61 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
DB2 does not accept a correlated subquery as order expression: "The sort-key-expression cannot include a correlated scalar fullselect (SQLSTATE 42703)". So the ordering on a field of a
@OneToManyor@ElementCollectionfrom #61 fails on DB2 with all three providers. OptimusFaces CI shows this intestLazyWithOneToManyon DB2.On DB2 only, when the page is ordered on a field of a
@OneToManyor@ElementCollectionwhich would otherwise be ordered via the correlated subquery, the page is now loaded in two queries:SELECT e.id ... LEFT JOIN e.collection c ... GROUP BY e.id ORDER BY MIN(CASE WHEN <criteria on the collection> THEN c.field ELSE NULLIF(c.field, c.field) END), e.id, with offset and limit. It applies the same restrictions as the entity query, and also theEXISTSsubquery of the count query when aQueryBuilderis used.id IN (ids). The entities are then put in the order of the IDs.Provider constraints:
ORDER BYclause. So the criteria on the collection also appear in aWHEREpredicate which is always true:(criteria OR e.id IS NOT NULL).NULLliteral in aCASE, soNULLIF(field, field)is used instead.Known limitations:
Page.ALL,id IN (ids)is not bounded. EclipseLink'sPostponedFetchRootalready does the same.Tests:
@ElementCollection.OptimusFacesDB2ITpasses on a local DB2 container with all three servers.Follow-up to #61
🤖 Generated with Claude Opus 5.5