Skip to content

fix(hibernate5): fetch join of to-one associations - #17055

Draft
borinquenkid wants to merge 7 commits into
7.2.xfrom
fix/h5-fetch-join-element-collection-7.0.x
Draft

borinquenkid wants to merge 7 commits into
7.2.xfrom
fix/h5-fetch-join-element-collection-7.0.x

Conversation

@borinquenkid

@borinquenkid borinquenkid commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Depends on #17071 (the split of #16853: a join type in the fetch argument): merge it first. This pull request is a draft until then. The two fix different bugs and conflict in GrailsHibernateQueryUtils and PagedResultList. This branch does not carry #17071's code yet; it is merged here once it reaches 7.2.x, and the conflict is resolved so that the call's fetch argument wins over the mapping:

  • The fetch loop and PagedResultList are Apply a join type in the fetch argument of list and the example queries #17071's: 'join' is a left join and a JoinType applies as requested. This branch's inner 'join' for a to-one association and its count conversion are dropped, so list(fetch: [destination: 'join']) and its total no longer leave out the instances without a destination.
  • The mapping's fetch join is applied after the sort, skips every association the argument names, and reuses a fetch that a nested path made.

A query fix for a long-open issue, for Hibernate 5. This is part of the Hibernate 5 half of #16846, split out after the dev@ discussion on fixes that change results: it changes no result that worked. The criteria blocks over collections of basic values (#14374), which an earlier version of this pull request carried, are no longer here. The Hibernate 7 half stays on #16846 against 8.0.x.

  • Domain class mapping with fetch: 'join' on OneToOne association results in n+1 queries #14343 A to-one association mapped with fetch: 'join' is loaded in the same select by findAll(), list() and criteria queries, so reading it for each row no longer costs one extra query per row. An association without the mapping stays lazy, and an association that the fetch: argument names follows the argument, not the mapping. The rows returned are the same; only the number of statements drops.

FetchJoinToOneSpec and a scenario in the example app cover it; with the main sources of 7.2.x, 5 of the 24 features of that spec fail. grails-data-hibernate5-core (1406) and the integration tests of the example app (9) pass, and so does codeStyle.

Fixes #14343

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

…basic-value collections

A to-one association mapped with fetch: 'join' is loaded in the same select by findAll(), list() and
criteria queries. A criteria block named after a hasMany collection of enums or other basic values no
longer throws MissingMethodException; it restricts the values through elements.

Fixes #14343
Fixes #14374

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@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. An explicit fetch join of a to-one association still does not fetch it.

@SuppressWarnings("rawtypes")
private static void fetchJoinMappedToOneAssociations(PersistentEntity entity, Root queryRoot, Set explicitFetches) {
for (PersistentProperty property : entity.getPersistentProperties()) {
if (property instanceof ToOne && !(property instanceof Embedded) && !explicitFetches.contains(property.getName()) &&

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.

destination is mapped to be fetched with a join, and the call also asks to fetch it. This skips that association because the call already named it. The other path joins it and does not fetch it, so a fresh session still loads destination later. Fetch the association the call asked to fetch.

…a join fetches it

fetch: [destination: 'join'] joined the association without fetching it, and the mapped fetch join skipped
it because the call had named it, so a fresh session loaded the association later, one statement per row.
A to-one association that the call asks to fetch with a join is fetched with the rows; any other
association is joined as before.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley: fetch: [destination: 'join'] fetches a to-one association now, whether or not the mapping already says fetch: 'join'. The call's own fetch used to be a plain join, and the mapped fetch join skipped the association because the call had named it, so a fresh session loaded it later. A to-one association that the call asks to fetch with a join is fetched with the rows; collections and embedded properties are joined as before. FetchJoinToOneSpec lists the flights with fetch: [destination: 'join'] (mapped) and fetch: [origin: 'join'] (not mapped) and expects at most two statements for the flights and their cities; both rows fail without the change.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@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. The total of a page that fetches an association with an inner join counts rows the page left out.

totalCount = hibernateTemplate.execute(new GrailsHibernateTemplate.HibernateCallback<>() {
public Integer doInHibernate(Session session) throws HibernateException, SQLException {
// the list fetched the associations its mapping joins, and a count has no entity to fetch them into:
// Hibernate rejects a fetch whose owner is not in the select list

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.

A list fetches destination with an inner join, so a flight with no destination is left out. The total still counts that flight, because this drops the join. Keep the flight out of the total too.

@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley: the total of a page now counts the flights its join keeps. The count query used to drop every fetch of the page's query; it now turns each fetch into a join of the same type, nested ones too, so a flight with no destination is out of the total when the page's inner join leaves it out. The new joins get names of their own: the query's generated aliases were fixed when the page ran, and the joins reused the root's.

Before asking for another look, I ran the paged list, a paged where query and a paged criteria, each with no fetch, with destination (mapped with a join), with origin, and with both, over five flights (one without a destination, one without an origin), and compared each total with the size of the same query unpaged. 12 rows, 3 of which fail without this change. The routes do not agree on the join: list(fetch: ...) fetches with an inner join, as it did before this pull request, while the where query and the criteria use an outer one, so their lists keep all five flights, and so do their totals. I left that difference as it was.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@borinquenkid
borinquenkid marked this pull request as draft November 23, 2026 00:07
The 7.0.x patch line takes no more fixes after the 2026-11-25 releases;
this PR moves to 7.2.x.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid
borinquenkid changed the base branch from 7.0.x to 7.2.x November 26, 2026 02:49
…ement-collection-7.0.x

# Conflicts:
#	grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/query/GrailsHibernateQueryUtils.java
An association that the fetch argument of list names is not fetched from
its fetch: 'join' mapping. Tests pin that, and that a sort by a
join-mapped association returns the rows it returns without the mapping.
The mapped fetch is now added after the sort, so that a sort never goes
through that left fetch.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

@jdaugherty on your note in #17071: the explicit fetch argument wins.

That merge goes in here once #17071 reaches 7.2.x. It also replaces what I told @jamesfredley on 11-20 about totals: 'join' no longer leaves rows out.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

… this change

A criteria block named after a collection of basic values behaves as on
7.x again. The change, its specs and its reference doc paragraph are
removed; the fetch join of to-one associations is unchanged.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid borinquenkid changed the title fix(hibernate5): fetch join of to-one associations and criteria over basic-value collections fix(hibernate5): fetch join of to-one associations Nov 30, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants