Repository navigation
fix(hibernate5): fetch join of to-one associations - #17055
borinquenkid wants to merge 7 commits into
Conversation
…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
jamesfredley
left a comment
There was a problem hiding this comment.
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()) && |
There was a problem hiding this comment.
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
|
@jamesfredley: |
jamesfredley
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
…ts the rows its join keeps Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@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 |
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
…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
|
@jdaugherty on your note in #17071: the explicit
That merge goes in here once #17071 reaches 7.2.x. It also replaces what I told @jamesfredley on 11-20 about totals: |
… 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
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
GrailsHibernateQueryUtilsandPagedResultList. 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'sfetchargument wins over the mapping:PagedResultListare Apply a join type in the fetch argument of list and the example queries #17071's:'join'is a left join and aJoinTypeapplies as requested. This branch's inner'join'for a to-one association and its count conversion are dropped, solist(fetch: [destination: 'join'])and its total no longer leave out the instances without a destination.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.
fetch: 'join'on OneToOne association results in n+1 queries #14343 A to-one association mapped withfetch: 'join'is loaded in the same select byfindAll(),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 thefetch:argument names follows the argument, not the mapping. The rows returned are the same; only the number of statements drops.FetchJoinToOneSpecand 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 doescodeStyle.Fixes #14343
https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL