Repository navigation
fix(hibernate5): a setting keeps the entities without the association for an association block in or - #16931
fix(hibernate5): a setting keeps the entities without the association for an association block in or#16931borinquenkid wants to merge 17 commits into
Conversation
…ther branch matches Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…e entities without the association The criteria guide documents an association block inside or as one branch: accounts that have either performed transactions OR were recently created. The block was an inner join, so an account without transactions was dropped before the other branch could match it. A block that has no join type of its own, inside an or, is now a left join. Fixes #16924 Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jamesfredley
left a comment
There was a problem hiding this comment.
Not approving. An association block that accepts a null now matches an entity that has no association. A later explicit inner join also reuses that left join.
…ociated entity A null test in the block matched the entities without the association, because the left join gives null for the missing one. The block now also requires the id of the joined alias. The shared block cases skip Neo4j until #16836 is merged. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jamesfredley
left a comment
There was a problem hiding this comment.
A null test in an association block no longer matches an entity that has no association. Thread PRRT_kwDOAAfRJ862Qfia resolved. Not approving. An empty association block no longer requires the association, and a later inner join reuses the left join.
…uires an associated entity, and an empty block does too A block outside an or that reuses the left join of a block in one no longer returns the entities without the association, and an empty block in an or keeps requiring an associated entity of the whole entity, as its inner join did.
jamesfredley
left a comment
There was a problem hiding this comment.
An empty association block requires the association again, and a later inner join no longer reuses the left join as if the child were optional. Threads PRRT_kwDOAAfRJ862VrVg and PRRT_kwDOAAfRJ862VrVl resolved. Not approving. A later block that asks for a left join is treated as if it required the child.
…r not an or joined the association with a left join
or { child {...}; eq(...) }; child(LEFT_JOIN) { isNull('name') } keeps the entity without a child, as the left join the
second block asked for does.
jamesfredley
left a comment
There was a problem hiding this comment.
A later block that asks for a left join keeps that left join. Not approving. A later block that asks for an inner join still reuses the left join.
| // a block that is a branch of an "or" asks for an associated entity, and so does a block that | ||
| // reuses the left join of one, as a block outside an "or" always did | ||
| // (a join type the caller named is kept as the caller named it) | ||
| final boolean requiresAssociated = branchOfOr || (!hasMoreThanOneArg && leftBlockAliases.contains(blockAlias)); |
There was a problem hiding this comment.
or { child { eq('name', 'kid') }; eq('name', 'orphan') }; child(CriteriaSpecification.INNER_JOIN) { isNull('name') }, where orphan has no child. The list returns orphan. The later block asked for an inner join. Require the child.
…and is no branch of an or CriteriaOrAssociationBlockSpec expects an association block without criteria to leave out the entities that have no associated entity, and to add no branch to the or it stands in, as on the datastores that join. The in-memory store matched nothing for the block and let the other branch of the or find the orphan. The block is now skipped as a branch and the whole result is restricted to the entities that have an associated entity. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
| // a block that is a branch of an "or" asks for an associated entity, and so does a block that | ||
| // reuses the left join of one, as a block outside an "or" always did | ||
| // (a join type the caller named is kept as the caller named it) | ||
| final boolean requiresAssociated = branchOfOr || (!hasMoreThanOneArg && leftBlockAliases.contains(blockAlias)); |
There was a problem hiding this comment.
with has a child named kid. orphan has no child. or { child { eq('name', 'kid') }; eq('name', 'orphan') } then child(INNER_JOIN) {} should return only with. It returns with and orphan. The second block reuses the left join from the or, and the extra argument turns off the existence check. Keep the inner join when the caller asks for one.
…d a block in an or of a detached criteria keeps its left join
child(INNER_JOIN) { } after an or reused the left join the or had made for the same association and, as it named a join
type, skipped the existence check, so an entity without the association stayed in the result; a block that names an
inner join asks for an associated entity now. The query path of a detached criteria had no rule for a block that is a
branch of an or at all, so an entity without the association dropped out of the or, an empty block did not ask for an
associated entity and a block after the or did not either; it makes a left join for such a block, checks for an
associated entity as criteria does, and does not count a negation as an or. CriteriaOrAssociationBlockSpec runs
each row through criteria and through a detached criteria, and has the explicit join type rows.
Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley Fixed in the new commit. Your case. The same rule through the other entry point. I ran every row of that spec through a detached criteria as well, and four of the eight earlier rows failed there: the query path used by detached criteria had no rule for a block that is a branch of an The H5 module (705 tests), the in-memory store and |
…nd a path a block repeats keeps one join
getOrCreateAlias reused the join of any earlier block of the same name whenever the block came with an alias, which a
block of a detached criteria always does, so dam { sire { ... } } filtered on the join of the root sire block and
returned nothing, and a second dam.sire block asked Hibernate for the same association path twice and failed with
a duplicate association path. The join made for the very path of the block is reused now, and a join made for the
same name under another path is not. The pedigree shapes of CriteriaNestedShapesSpec are checked through a detached
criteria as well, with the repeated nested blocks; the blocks that are branches of an or stay on criteria until #16931.
Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…d criteria The detached criteria path keeps a left join for a block that is a branch of an or since #16931 (merged into this branch), so the three shapes that were checked through criteria only are checked through both. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. An explicit inner join after one or is kept. The same join inside a second or still lets an entity without the association through.
| } | ||
| else if (requiresAssociated) { | ||
| // a missing associated entity is a null that a null test in the block would match | ||
| logicalExpression.args.add(0, Restrictions.isNotNull(blockAlias + ".id")); |
There was a problem hiding this comment.
with has a child named kid. orphan has no child. An or that asks for the child, then a second or with child(0), should return only with. It returns with and orphan. The existence check sits inside the second or, so the name branch bypasses it. Keep that check on the whole query.
…ation on the whole query A block that names an inner join and reuses the left join of a block in an earlier or kept its existence check inside its own expression, so a branch of the second or that does not mention the association bypassed it. The caller asked for an inner join, so the check is on the whole query, as for a block without criteria. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…an or Ten shapes of a block that follows an or asking for the child: at the top level, as a branch of a second or, and in a not, each with a plain, an inner join and a left join block, and with and without criteria. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…or it after a block that named a left join Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley: I ran every ordered pair of nine kinds of association block (left join, left join with a restriction, inner join, inner join with a restriction, plain, plain with a restriction, an |
|
@jdaugherty: the Hibernate 5 counterpart of #16922, for #16897: an association block inside |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. An explicit inner join inside an or still lets an entity without the association through when an earlier block joined it.
| final boolean explicitInnerJoin = hasMoreThanOneArg && | ||
| joinType == org.hibernate.sql.JoinType.INNER_JOIN.getJoinTypeValue(); | ||
| final boolean requiresAssociated = branchOfOr || | ||
| (leftBlockAliases.contains(blockAlias) && (!hasMoreThanOneArg || explicitInnerJoin)); |
There was a problem hiding this comment.
An earlier manager block joins manager. Then two or blocks follow, and the second asks for manager as an inner join. orphan, who has no manager, still comes back. The earlier join is not recorded, so the inner join reuses it and adds no check. Keep orphan out.
|
@jamesfredley I ran your manager shape. The orphan comes back when the association points at its own class (manager is an Employee), and plain 7.0.x returns the same results, so the PR did not change it. The block is joined with a left join by the "same table" rule and nothing records it. The same shapes on an association to another class are correct on this head: an empty block that names an inner join asks for the association on the whole query. I filed it as #17113 and kept it out of this PR. |
…ation after a default left join on the entity's own class, and the note for the or branch Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
James, found it. The entity has an association of its own class, like your |
Port the left join of an association block in an or onto the deferred joins: createJoin makes the join of an or branch a left join unless the query asks for another type. Three base specs pinned the inner join of an or branch; they now expect the rows an or branch keeps. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
Refreshed against 7.0.x after the merge-up (merge commit ce1ec87). The base now makes joins when the query runs (deferred joins), so the left join of an association block in an Three specs that came in with the merge-up pinned the inner join of an or branch, which is the result this PR changes, so I changed their expectations rather than the fix:
Verified on ce1ec87: H5 core 1406 tests, 0 failures; datamapping-core-test 585, 0 failures; codeStyle clean. With the fix reverted, 12 specs fail. This changes a result on 7.0.x, so it needs two approvals. |
matrei
left a comment
There was a problem hiding this comment.
First round, at ce1ec87. The branch merges cleanly with 7.0.x at ffd848b. CI hasn't run: the no ci label skipped all 30 checks.
I ran the 46 queries of my review of #16930 at this head and on 7.0.x:
- with Hibernate 5;
- with the in-memory store, plus seven blocks without criteria.
The fixture:
- the teams
ArsenalandPlayerlessplay for the clubArsenal,ChelseaandSquadforChelsea, andMUforManchester United; - the teams
UnattachedandEmptyhave no club; Squadhas the playersSquad A,Squad BandSquad C,PlayerlessandEmptyhave none, and the other teams have one each;- the club
Teamlesshas no team, and the playerFree Agenthas no team.
I agree with the fix. The criteria guide describes the or as either branch, and the in-memory store already returns the new rows for every query in the first table. Apart from the to-many rows below, #16930 returns the same rows with Hibernate 7.
The fix changes a result, so #17061 applies
| 7.0.x | This head | |
|---|---|---|
Team.createCriteria().list { or { club { eq('name', 'Arsenal') }; eq('name', 'Unattached') } }, also as a detached criteria, with count(), and with max: 10 and its totalCount |
Arsenal, Playerless |
Arsenal, Playerless, Unattached |
Team.where { club.name == 'Arsenal' || name == 'Unattached' }.list(), also with club { name == 'Arsenal' } |
Arsenal, Playerless |
Arsenal, Playerless, Unattached |
or { isNull('club'); club { eq('name', 'Arsenal') } } |
Arsenal, Playerless |
also Unattached and Empty |
not { or { club { eq('name', 'Arsenal') }; eq('name', 'MU') } } |
Chelsea, Squad |
also Unattached and Empty |
Player.createCriteria().list { or { team { eq('name', 'Squad') }; eq('name', 'Free Agent') } } |
the three players of Squad |
also Free Agent |
Team.createCriteria().get { or { club { eq('name', 'Teamless') }; eq('name', 'Empty') } } |
null |
Empty |
borinquenkid's comment of 11-28 says that this changes a result on 7.0.x and needs two approvals. Under "Fixes That Change a Result" in CONTRIBUTING.md, it's also a wrong result that many applications get:
- an association block in an
or, and a where query with||over an association, are common queries; - no other query API of Hibernate 5 returns the new rows yet: the where query returns the old ones too.
So it needs a setting:
- Default: off on 7.0.x, 7.1.x, 7.2.x and 8.0.x, on from 8.1.0, and removed in 9.0.0.
- Key: the same as in #16930, which makes the same fix for Hibernate 7. This branch is the fix for the oldest branch with the bug, so it goes first.
- Warning: a warning at startup when an application sets the setting to keep the old rows.
It also needs:
- the description to say which results change, how an application gets the old ones, and why there's a setting;
- a row in
compatibilitySettings.adoc; - an issue for each of the two later steps.
The note and the skill entry are there. They speak only of a block in a criteria query, though. The where query changes too, as the expectation that the merge changed in AssociationJoinTypeSpec shows, so the note should name it.
A collection holds only the elements that the block matches
The left join of a block on a collection makes Hibernate fetch the collection from the rows of the join, and the block's criterion has left out the other elements:
| 7.0.x | This head | |
|---|---|---|
Team.createCriteria().list { or { players { eq('name', 'Squad A') }; eq('name', 'Empty') } }, also as a where query players.name == 'Squad A' || name == 'Empty' |
Squad, its players not loaded |
Squad, holding only Squad A, and Empty |
or { club { eq('name', 'Arsenal') }; players { eq('name', 'Squad A') } } |
Arsenal, Squad |
also Playerless, and Squad holds only Squad A |
- Why it matters: the session keeps that
Squaduntil it's cleared, so the rest of the request sees a team with one player. - Not new for an explicit left join: a block that names one,
players(1) { eq('name', 'Squad A') }, does the same on 7.0.x. - New for a block without a join type: this branch makes it the default for a block in an
or. - Hibernate 7 at #16930: it doesn't load the collection there.
For a collection, a restriction with a subquery would keep the instances without elements and fill no collection. On 7.0.x, a join type requested for a path that ends in a collection, or passes through one, already restricts the query that way. A test for this case should check the elements of the collection that the query returns.
A not with more than one criterion
The code doesn't count the criteria of a not as branches of an or ("they are not branches of an or: a block in one keeps its join"). But not { a; b } means not (a or b), and the two forms differ at this head:
| 7.0.x | This head | In-memory store, both | |
|---|---|---|---|
not { or { club { eq('name', 'Arsenal') }; eq('name', 'MU') } } |
Chelsea, Squad |
Chelsea, Squad, Unattached, Empty |
Chelsea, Squad, Unattached, Empty |
not { club { eq('name', 'Arsenal') }; eq('name', 'MU') } |
Chelsea, Squad |
the same as on 7.0.x | Chelsea, Squad, Unattached, Empty |
I'd treat a not of more than one criterion as an or, here and in #16930. If not, the note should say that only or keeps the entities without the association.
The in-memory store
The change to SimpleMapQuery changes what the in-memory store returns for a block without criteria. So the unit tests of an application that use the store can get other rows:
| 7.0.x | This head | |
|---|---|---|
Team.createCriteria().list { club { } }, also in and or or, and as a detached criteria |
no team | the 5 teams with a club |
Team.createCriteria().list { players { } } |
no team | the 5 teams with players |
or { club { }; eq('name', 'Unattached') } |
Unattached |
no team |
club { }; or { club { eq('name', 'Chelsea') }; eq('name', 'Unattached') }, also with club { } after the or |
no team | Chelsea, Squad |
These are now the rows of Hibernate 5, which is the right direction for a store that unit tests use in place of Hibernate. The description and the note should say so.
Smaller points
- The open #16853 parts: this branch conflicts with #17070 to #17073 in
AbstractHibernateCriteriaBuilder.java. They're ahead in the queue, so this branch will need another merge with 7.0.x after them, and I'll run the rows again then. - The earlier reviews' points that I ran:
or { club { isNull('name') }; eq('name', 'nobody') }returns nothing;or { club(0) { eq('name', 'Arsenal') }; eq('name', 'Unattached') }keeps the inner join;club { }before or after theorstill requires the club.
- James's case on an association to its own class: #17113 has it, as the comment of 11-21 says.
What I ran
- Hibernate 5: at this head and on 7.0.x (ffd848b).
- The in-memory store: in
grails-datamapping-core-test, at the same two heads. - Hibernate 7: for the comparison with #16930, at d6fa6db.
As a fix that changes a result, this needs a second approval under #17061.
….leftJoinAssociationInOr, and fill no partial collection A block in an or that joins its association with a left join changes the result of queries that many applications run, so it is behind a setting that is off by default. The left join of a collection for such a block is restricted, so Hibernate does not fill the collection with only the elements that the block matches. Add the setting to the compatibility page, the upgrade note for 7.2.6 and the upgrade skill. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…ows of a block on the entity's own class A criteria block on an association of the entity's own class is left-joined without the setting and asks for no associated entity. With the setting it was asked for one as a branch of an or, so the query lost rows. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@matrei Thanks, done in 4fb4be1 and cca0a1f.
https://github.com/apache/grails-core/blob/cca0a1f4f2/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/query/AbstractHibernateQuery.java#L642-L644 |
✅ All tests passed ✅🏷️ Commit: cca0a1f Learn more about TestLens at testlens.app/docs. |
An association block inside
or { }is one branch of the disjunction. The criteria guide documents it that way: "all accounts that have either performed transactions in the last 10 days OR have been recently created in the last 10 days". The block is an inner join, so an account without transactions is dropped before the other branch can match it. A where query with||over an association does the same.Results that change, and the setting
Association blocks in
orare common, and no other query API of Hibernate 5 returns the new rows, so under "Fixes That Change a Result" inCONTRIBUTING.mdthe fix is behind a setting,grails.gorm.leftJoinAssociationInOr:false, today's resultstrue(planned)Setting it to
falseexplicitly logs a startup warning naming Grails 9.With
true:or, in a criteria query, a detached criteria or a where query with||, is a left join: the instances without the association are found by the other branches, also bycountand thetotalCountof a paged list.orand a block directly in anotrequire the association as before. In a criteria query, a block on an association of the entity's own class, left-joined by default, keeps its rows as before. Anotis not treated as anor, because the criteria builder joins a block as it enters it, before it knows how many criteria thenotholds.With or without the setting:
To keep the previous results of an
or, leave the setting unset.Tests
CriteriaOrAssociationSettingSpec(Hibernate 5) runs a to-one association, a collection and a collection in a join table through a criteria query, a detached criteria, a where query,countand a paged list, with the setting unset,falseandtrue, and reads the collections of the instances returned. The unset andfalserows for those queries also pass on plain 7.2.x.CriteriaOrAssociationSpecandCriteriaOrAssociationBlockSpec(MongoDB skipped, it has no association blocks) run the block rows; on Hibernate 5 with the setting.HibernateDatastoreSpecpins the warning.grails-data-hibernate5-core,grails-datamapping-core-test,grails-datastore-core,grails-core,grails-data-mongodb-coreandcodeStylepass.CriteriaOrAssociationBlockSpecalso runs on Neo4j and passes there with #16836, so merge #16836 first. The open parts of #16853 touch the same builder, so this needs another merge after them.Fixes #16924
Evidence and the doc passage: #16924 (comment)
https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL