Skip to content

Keep a join type that a fetch mode replaced for the joins of a criteria query - #17192

Merged
jdaugherty merged 2 commits into
apache:7.0.xfrom
jdaugherty:fix/h5-builder-superseded-join-type
Nov 27, 2026
Merged

jdaugherty merged 2 commits into
apache:7.0.xfrom
jdaugherty:fix/h5-builder-superseded-join-type

Conversation

@jdaugherty

@jdaugherty jdaugherty commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Description

With GORM for Hibernate 5, a where query or a detached criteria keeps a join type that it requests for the join of its criteria when it requests a fetch mode for the association afterwards: select('author') after join('author', JoinType.LEFT) keeps the left join for the criteria on author, as the 7.0.18 upgrade notes say. A criteria query dropped the join type instead, so the join of an association block or of an alias fell back to an inner join. With five teams, one of them without a club:

// returns the two Arsenal teams and the team without a club
Team.where { club.name == 'Arsenal' || name == 'Unattached First Team' }
    .join('club', JoinType.LEFT)
    .join('club')
    .list()

// returned only the two Arsenal teams; now returns the team without a club as well
Team.createCriteria().list {
    join('club', JoinType.LEFT)
    join('club')
    or {
        club { eq('name', 'Arsenal') }
        eq('name', 'Unattached First Team')
    }
}

The same holds for select('club'), fetchMode('club', FetchMode.JOIN) and the fetch argument of list(Map) { } in place of join('club'), for an alias made with createAlias('club', 'c'), and for count and the totalCount of a paged list. With #17079, GORM for Hibernate 7 returns the team in each of these.

The criteria builder now keeps a join type that join(String), select(String), fetchMode or the fetch argument replaced, and applies it to the join that the criteria make for the association once the criteria are built. It makes no join of its own, so without criteria on the association the fetch mode alone decides, as before: join('club', JoinType.INNER); join('club') still returns every team. A join type requested after the fetch mode, with join(String, JoinType) or in the fetch argument, replaces it.

Start reading at AbstractHibernateCriteriaBuilder.supersedeJoinType and applyRequestedJoinTypes. The builder finds the join of the criteria by its path from the queried class, also for a nested block and for an alias made through another alias, and makes it again with the join type under the same alias, with the code that GrailsHibernateUtil.applyJoinType uses for a requested join type. It applies the replaced join types before the requested ones, which make joins of their own for the associations before the requested one, so a replaced join type never changes those: join('parent', JoinType.INNER); join('parent'); join('parent.parent', JoinType.LEFT) keeps the category without a parent, as a where query does. The change adds one public method, GrailsHibernateUtil.applyJoinTypeToExistingJoin, marked @Internal like applyJoinType.

Results that change

A criteria query that requests JoinType.LEFT or JoinType.RIGHT for an association, then a fetch mode for the same association, and has criteria on it now also returns the instances that the requested join keeps, as a where query does. Requested join types in a criteria query are new in 7.0.18 (#17069); on 7.0.17 the criteria's join was always an inner join. To get that result, leave out the requested join type, or request JoinType.INNER. There is no setting, because the change makes a criteria query return what a where query already returns for the same calls.

The 7.0.18 upgrade note on select('author') after a requested left join, the same entry of the upgrade skill and the GORM guide's section on fetching now say that this holds for a criteria query as well. The guide said that a criteria query keeps the join of its criteria.

Contributor Checklist

Please review the following checklist before submitting your pull request. Pull requests that do not meet these requirements may be closed without review.

Issue and Scope

  • This PR is linked to an existing issue that has been acknowledged or approved by the project team. If no approved issue exists, please give background on why this change is necessary. Tickets are preferred for release change log history.
  • This PR addresses the complete scope of the linked issue. Partial implementations or unfinished work should not be submitted for review.
  • This PR contains a single, focused change. Unrelated changes should be submitted as separate pull requests.
  • This PR targets the correct branch for the type of change (see Choosing the Branch):
    • Maintenance branches, whose minor version has been released and that still get patch releases (e.g., 7.0.x, 7.2.x, 8.0.x): Bug fixes only, released as the next patch version. No new features or API changes. A bug fix targets the oldest release branch that has the bug and still gets releases, and is merged up.
    • Next minor branch, until its minor version is released (e.g., 8.1.x before 8.1.0): New features are welcome, but breaking existing APIs must be avoided.
    • Next major branch, until its major version is released (e.g., 9.0.x before 9.0.0): Reserved for major changes. Breaking API changes are permitted.
  • If this PR changes a result, it follows Fixes That Change a Result. A result is what a call that works today returns, stores, renders, binds or maps to, and a fix that makes such a call throw changes one too.
    • The description says which results change, how to get the old ones, and whether the fix needs a setting, and why.
    • The release notes and the upgrade guide section of the release that carries it have a note, and the upgrade skill (grails-skills/upgrade/skills/grails-upgrade/SKILL.md) has the same entry.
    • A setting it adds has a row on the Compatibility Settings page, and an issue for each later step: turning it on and removing it.
    • On a maintenance branch or the next minor branch, it waits for two approving reviews.

Code Quality

  • I have added or updated tests that cover the changes introduced in this PR. All code contributions are expected to include appropriate test coverage.
  • I have verified that all existing tests pass by running ./gradlew build --rerun-tasks.
  • My code follows the project's code style guidelines. I have run ./gradlew codeStyle and resolved any violations. See Code Style for details.
  • This PR does not include mass reformatting, style-only changes, or large-scale refactoring unless it was explicitly approved in the linked issue. Unsolicited reformatting will not be accepted.
  • If generative AI tooling was used in preparing this contribution, a quality model was used to ensure contributions are consistent with the project's quality standards.

Licensing and Attribution

Documentation

  • If this PR introduces user-facing changes, I have included or updated the relevant documentation.
  • If this PR adds a new feature, I have updated the What's New section of the Grails Guide.
  • If this PR introduces breaking changes or changes that require user action during an upgrade, I have updated the Upgrade Notes for the corresponding version in the Grails Guide.
  • The PR description clearly explains what was changed and why.

First-time contributors: Please read our Contributing Guide before submitting.
Pull requests that appear to be auto-generated, incomplete, or unrelated to an approved issue may be
closed to help maintainers focus on reviewed and planned work. We appreciate your understanding.

…ia query

A where query and a detached criteria keep a join type that they
request for the join of their criteria when they request a fetch mode
for the association afterwards: select('author') after
join('author', JoinType.LEFT) keeps the left join for the criteria on
author. The criteria builder dropped the join type instead, so the join
of an association block or of an alias fell back to an inner join, and
join('club', JoinType.LEFT); join('club'); or { club { ... }; ... }
left out the team without a club that the same where query returns.

The builder now keeps a join type that join(String), select(String),
fetchMode or the fetch argument of list replaced, and applies it to the
join that the criteria make for the association once the criteria are
built. It makes no join of its own, so without criteria on the
association the fetch mode alone decides, as before, and a join type
requested after the fetch mode replaces it.
@jdaugherty
jdaugherty force-pushed the fix/h5-builder-superseded-join-type branch from 1dd2293 to c7f5fa1 Compare November 27, 2026 14:09

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A left join opened through a chained alias is still an inner join, and a page of one row can leave a collection with only the row that fit.

sbglasius
sbglasius previously approved these changes Nov 27, 2026

@sbglasius sbglasius left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an AI assisted review

LGTM. The fix mirrors GrailsHibernateUtil.applyJoinType, and the spec covers block, nested, alias, chained alias, paging and count. Agree with deferring the t.club path and paged collections to #17195/#17196.

Nit: applyToCriteriaJoin resolves the subcriteria through aliasMap and repeats the jakarta→Hibernate join-type mapping. GrailsHibernateUtil already has both: findCriteria/getWholePath and the mapping. Sharing them would avoid the drift. Also, the aliasMap loop could break on the first match.

borinquenkid
borinquenkid previously approved these changes Nov 27, 2026

@borinquenkid borinquenkid left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an AI assisted review

LGTM. Reviewed at c7f5fa1. I read the diff and ran extra cases against the head in a scratch checkout; none of them found a defect.

What I ran, on top of AssociationJoinTypeSpec (all pass): an alias and a block for the same path; a parent and a child path both requested, in both orders (team then team.club, and team.club then team), with createAlias('team','t') and createAlias('t.club','c'); the parent alone with a left-joined child alias; listDistinct; get; a sort by club.name; and a second club block after the or. The remove-and-recreate of the subcriteria in applyToCriteriaJoin moves it to the end of the list, and none of these forms depends on the order. I read the chained-alias thread the same way as @jdaugherty: join('team.club', JoinType.LEFT) with createAlias('t.club', 'c') works, and join('t.club', ...) is #17195, which fails the same on 7.0.x without this change.

Two small things:

  1. @sbglasius's nit stands: applyToCriteriaJoin repeats the jakarta-to-Hibernate join type mapping and the alias lookup that GrailsHibernateUtil already has, and the aliasMap loop can break on the first match.
  2. The description says Hibernate 7 returns the team in each of these, citing #17079, which is still open. Say "with #17079" so a reader does not take it as released behaviour.

Checklist: the linked-issue box and the ./gradlew build --rerun-tasks box are unticked. As a result change on a maintenance branch it needs two approvals; with this one it has two.

…n type does

The criteria builder found the join of the criteria for an association whose
join type a fetch mode replaced through its own alias map, and mapped the join
type to Hibernate's and made the join again with code of its own.
GrailsHibernateUtil.applyJoinType already finds a join of the criteria by its
path from the root, also through an alias made through another alias, maps
the join type and makes the join again, so the builder now uses the same code
through GrailsHibernateUtil.applyJoinTypeToExistingJoin.

The replaced join types are applied before the requested ones, which make
joins of their own for the associations before the requested one, so that a
replaced join type applies only to a join that the criteria make, as before:
join('parent', JoinType.INNER); join('parent'); join('parent.parent',
JoinType.LEFT) keeps the category without a parent, as the where query does.
@github-actions
github-actions Bot dismissed stale reviews from sbglasius and borinquenkid November 27, 2026 16:43

Dismissed automatically: the pull request changed after this approval, from c7f5fa1 to 9bd0c23.

@jdaugherty
jdaugherty merged commit ffd848b into apache:7.0.x Nov 27, 2026
18 of 27 checks passed
@jdaugherty
jdaugherty deleted the fix/h5-builder-superseded-join-type branch November 27, 2026 16:46
@matrei

matrei commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

An after-merge review. I was reviewing c7f5fa1 when this merged, and I re-ran my probes at the merge commit, ffd848b. CI didn't run the tests for this change: the PR had no run ci label, and a push to 7.0.x builds without tests. Locally, the tests of grails-data-hibernate5-core pass at the merge commit (1373 tests, 59 skipped).

9bd0c23 was pushed four minutes before the merge. The push dismissed both approvals, so the final head merged without one, and #17061 asks for two for a result change on a maintenance branch. I read 9bd0c23, and it holds for everything I probed. The rows are the same as at c7f5fa1, and its parent.parent order case is a good catch.

What works: for a criteria query with a block or an alias, a left join that a fetch mode replaced now gives the teams the where query gives. I checked nested blocks, join('club') inside a team { } block, get, listDistinct, withCriteria, count, totalCount, JoinType.RIGHT, and club { order('name') } from #17191. On 7.0.x before the merge, each of these left out the team without a club.

Before 7.0.18

1. A collection that the criteria have a block for is fetched with only its matching elements, and saving it deletes the others. The block's join is now a left join, and Hibernate 5 fetches the collection through it, as #17196 describes. No max is involved: the criteria on the collection filter its elements. The test used a collection kept in a join table (Collection<Item> items with static hasMany = [items: Item] and no belongsTo), holding items A and B. After the query, I added C to the fetched collection and saved:

Owner.createCriteria()…, then addToItems(new Item(name: 'C')).save(flush: true) 7.0.x before the merge 7.0.x now
get { join('items', JoinType.LEFT); join('items'); items { eq('name', 'A') } } items A, B, C items A, C
list { join('items', JoinType.LEFT); join('items'); or { items { eq('name', 'A') }; eq('name', 'Nobody') } } A, B, C A, C
get { join('items', JoinType.LEFT); items { eq('name', 'A') } } (#17069) A, C A, C
Owner.where { items { name == 'A' } }.join('items', JoinType.LEFT).join('items').get() A, C A, C

The second row is the form this PR fixes for the returned instances. With Team and players, the same calls give Squad Team with only Squad Player A in players, where 7.0.x before the merge left the collection lazy and complete. #17196 doesn't mention the save yet.

Both join types now go through GrailsHibernateUtil.rejoin, so one change there covers every criteria-query form. I tried this: when rejoin makes a left join without a with-clause, it passes Restrictions.sqlRestriction('1=1') as one, as #17070 does to keep a paged sort join from fetching the collection. With it:

  • the first three rows keep A, B, C, and the collections are complete and not fetched;
  • the returned instances are the same as at the merge commit for all of my team probes;
  • AssociationJoinTypeSpec passes.

The where query in the last row goes through AbstractHibernateQuery and still loses B. That's #17196. I applied the with-clause to every left join. A fix should apply it only to collections: a with-clause may stop the fetch that join('club') asks for on a to-one association, and I didn't check that. A spec row that saves a join-table collection after the query would cover it. Since the first two rows are new with this PR and the third with #17069, I'd fix them before 7.0.18, even if the rest of #17196 waits.

2. The sort argument still drops the left join, as in my comment on #17191. Without a block there is no join for the replaced type to apply to, and the sort makes its own inner join:

7.0.x before the merge 7.0.x now Detached criteria and where query
Team.createCriteria().list(sort: 'club.name') { join('club', JoinType.LEFT); select('club') }, also with join('club') in place of select('club'), or with fetch: [club: 'select'] 4 teams 4 5
list(sort: 'club.name') { join('club', JoinType.LEFT) } 5 5 5

With a club { } block, the sort uses the block's join and now keeps the 5 teams. The PR didn't link #17191, which stays open for these rows. #17070 handles the sort's join (applyRequestedSortJoins, isRequestedLeftSortJoin), so its rebase may be the easiest place to fix them (point 3).

3. #17070 to #17073 now conflict with 7.0.x. All four conflict in AbstractHibernateCriteriaBuilder, #17071 to #17073 also in the upgrade skill, and #17072 and #17073 also in fetching.adoc. #17070 keeps its own record of the same calls, explicitFetchModes, and clears requestedJoinTypes only after its orders are added. supersededJoinTypes is cleared in applyRequestedJoinTypes, before those orders, so the rebase can't simply keep both sides. Make the two records one and keep it until the orders are added. Then a replaced LEFT can count as a requested left join for the sort's join, which fixes point 2, and a replaced INNER does what explicitFetchModes does now.

4. The 7.0.18 note and "Results that change" don't mention:

  • the collections of point 1, if they ship;
  • a projection in a block: join('club', JoinType.LEFT); join('club'); projections { club { property('name') } } now includes a null for the team without a club (5 values, against 4 before);
  • with JoinType.RIGHT and a fetch mode, the list now has a null for a club without a team, and count counts it. The note's RIGHT bullet already says this for a where query, so a pointer to it would do.

Notes

  1. In fetching.adoc, "so the join type no longer leaves out any instance by itself" follows an example with JoinType.LEFT, which never leaves out an instance. "so a requested JoinType.INNER no longer leaves out an instance by itself" says what's meant.

  2. James's second inline point holds in part. The collection is incomplete, but the page isn't what cuts it (point 1). His first point is GORM for Hibernate 5: a join type requested through an alias path is not applied to the join of the alias #17195.

  3. 7.0.x was the right base: Apply a join type that a criteria query requests #17069 added requested join types to the criteria query in 7.0.18, which isn't released yet.

jdaugherty added a commit to jdaugherty/grails-core that referenced this pull request Nov 28, 2026
…r its association

createAlias without a join type requested an inner join for its path, which
replaced a join type that the query requested for the association before
the alias, and one that a later fetch mode had replaced, so
join('club', JoinType.LEFT); createAlias('club', 'c') left out the teams
without a club. Hibernate 5 joins such an alias with the requested type, as
the merged apache#17192 and the open apache#17070 specify. The alias now takes that type
and leaves the request and the fetch mode as they are. A join type that an
earlier alias of the same path configured is that alias's own, so a second
alias of the path still has an inner join of its own.

AssociationJoinTypeSpec gets the rows of the Hibernate 5 spec for an alias:
the alias form of a requested left join before, after and in the fetch
argument, the criteria-query feature of apache#17192 with an alias and an alias
made through another alias, and the order feature of apache#17070, also through
an alias.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants