Repository navigation
Keep a join type that a fetch mode replaced for the joins of a criteria query - #17192
Conversation
…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.
1dd2293 to
c7f5fa1
Compare
jamesfredley
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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:
- @sbglasius's nit stands:
applyToCriteriaJoinrepeats the jakarta-to-Hibernate join type mapping and the alias lookup thatGrailsHibernateUtilalready has, and thealiasMaploop canbreakon the first match. - 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.
|
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 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 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, Before 7.0.181. 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
The second row is the form this PR fixes for the returned instances. With Both join types now go through
The where query in the last row goes through 2. The
With a 3. #17070 to #17073 now conflict with 7.0.x. All four conflict in 4. The 7.0.18 note and "Results that change" don't mention:
Notes
|
…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.
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')afterjoin('author', JoinType.LEFT)keeps the left join for the criteria onauthor, 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:The same holds for
select('club'),fetchMode('club', FetchMode.JOIN)and thefetchargument oflist(Map) { }in place ofjoin('club'), for an alias made withcreateAlias('club', 'c'), and forcountand thetotalCountof 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),fetchModeor thefetchargument 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, withjoin(String, JoinType)or in thefetchargument, replaces it.Start reading at
AbstractHibernateCriteriaBuilder.supersedeJoinTypeandapplyRequestedJoinTypes. 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 thatGrailsHibernateUtil.applyJoinTypeuses 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@InternallikeapplyJoinType.Results that change
A criteria query that requests
JoinType.LEFTorJoinType.RIGHTfor 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 requestJoinType.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
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.8.1.xbefore 8.1.0): New features are welcome, but breaking existing APIs must be avoided.9.0.xbefore 9.0.0): Reserved for major changes. Breaking API changes are permitted.grails-skills/upgrade/skills/grails-upgrade/SKILL.md) has the same entry.Code Quality
./gradlew build --rerun-tasks../gradlew codeStyleand resolved any violations. See Code Style for details.Licensing and Attribution
Documentation