Repository navigation
Build criteria on a @OneToMany path as correlated subquery - #70
Merged
Merged
Conversation
A single criteria value on a field nested in a @onetomany was built as a JOIN on the main query, which returned an entity once per matching element on EclipseLink and OpenJPA. It is now built as correlated subquery in the data query, the same as multiple values. In the count query the JOIN stays, as it is inside the subquery selecting the entities to count, except for a negated value: a negated JOIN matches any other element instead of no element. The query is now DISTINCT only when it has a JOIN or fetch JOIN over a collection. Fetches postponed by PostponedFetchRoot are not part of the query, so they no longer make it DISTINCT. Fixes #69 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
OpenJPA's FetchPathImpl#getFetches() throws NullPointerException when the fetch has no fetches of its own, which hasCollectionJoins() ran into on a singular fetch such as getPageWithOwners(). 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 #69
A single criteria value on a field nested in a
@OneToManywas built as a JOIN on the main query. On EclipseLink and OpenJPA this returned an entity once per matching element, so a page held fewer distinct entities than its limit, and an entity could show up on two pages.BaseEntityServicenow builds such a criteria as correlated subquery viabuildArrayPredicate()in the data query, the same as multiple values. In the count query the JOIN stays, as it is inside the subquery selecting the entities to count. A negated value is always built as correlated subquery, because a negated JOIN matches any other element instead of no element. On OpenJPA a negated value is skipped in the count query, the same as multiple values, so the count may be too high there.DISTINCTonly when it has a JOIN or fetch JOIN over a collection (hasCollectionJoins()). Fetches postponed byPostponedFetchRootare not part of the query, so they no longer make itDISTINCT. This also matters for getPage() ignores ordering on a @OneToMany which is not fetched when PostponedFetchRoot is used #61, as aDISTINCTquery cannot be ordered by an expression which is not selected.buildElementPredicate(), for a@OneToManyand an@ElementCollectionalike.New tests in
OmniPersistenceIT:testSingleCriteriaOnOneToManyReturnsEachEntityOnce: two pages withphones.numberlike "1", with and without fetch, plus the count. On the code before this PR this fails on EclipseLink with 8 distinct of 20 persons.testNegatedSingleCriteriaOnOneToManyMatchesEntitiesWithoutMatchingElement.testSingleEntityCriteriaOnOneToMany.🤖 Generated with Claude Opus 5.5