Skip to content

Order by a collection field via an ID query on DB2 - #76

Merged
BalusC merged 2 commits into
developfrom
fix/db2-collection-order
Nov 3, 2026
Merged

BalusC merged 2 commits into
developfrom
fix/db2-collection-order

Conversation

@BalusC

@BalusC BalusC commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

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 @OneToMany or @ElementCollection from #61 fails on DB2 with all three providers. OptimusFaces CI shows this in testLazyWithOneToMany on DB2.

On DB2 only, when the page is ordered on a field of a @OneToMany or @ElementCollection which would otherwise be ordered via the correlated subquery, the page is now loaded in two queries:

  1. An ID query: 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 the EXISTS subquery of the count query when a QueryBuilder is used.
  2. The entity query, restricted to id IN (ids). The entities are then put in the order of the IDs.

Provider constraints:

  • EclipseLink and OpenJPA do not register parameters which occur only in the ORDER BY clause. So the criteria on the collection also appear in a WHERE predicate which is always true: (criteria OR e.id IS NOT NULL).
  • OpenJPA cannot render a NULL literal in a CASE, so NULLIF(field, field) is used instead.

Known limitations:

  • For Page.ALL, id IN (ids) is not bounded. EclipseLink's PostponedFetchRoot already does the same.
  • On OpenJPA, some criteria on a collection are weaker in a subquery. The count can therefore be too high, as it already was before.

Tests:

  • New ITs check that each matching entity is on exactly one page. They cover entities which match the criteria on a collection without having one element which matches all of them, and ordering on an enum field and on an @ElementCollection.
  • All OmniPersistence ITs pass on H2 with Hibernate, EclipseLink and OpenJPA, also with the ID query path forced on for all databases.
  • OptimusFacesDB2IT passes on a local DB2 container with all three servers.

Follow-up to #61

🤖 Generated with Claude Opus 5.5

BalusC and others added 2 commits November 3, 2026 09:11
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>
@BalusC
BalusC merged commit fea83d5 into develop Nov 3, 2026
3 checks passed
@BalusC
BalusC deleted the fix/db2-collection-order branch November 3, 2026 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant