Repository navigation
fix(hibernate7): an association block that is a branch of or keeps the entities without the association - #16930
borinquenkid wants to merge 7 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 restricts the association, in an or with more than one branch, is now a left join. A block outside an or, a block without criteria and the only branch of an or keep the inner 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. The shared cases also fail on a datastore that still uses an inner join.
| * performed transactions in the last 10 days OR have been recently created". An entity without the association is | ||
| * therefore found by the other branch. MongoDB has no association blocks. | ||
| */ | ||
| @IgnoreIf({ System.getProperty('mongodb.gorm.suite') == 'true' }) |
There was a problem hiding this comment.
The new shared cases run on a datastore that still uses an inner join for an association block. The to-one case returns only with, and the to-many case returns only Fred. Do not require that result from a datastore that has not changed.
…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 row of the join on a left join. 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_kwDOAAfRJ862Qj3a resolved. Not approving. The same null test still matches when the association block is inside another block, and the shared cases still fail on a datastore that uses an inner join.
| if (predicate == null) { | ||
| continue; | ||
| } | ||
| String path = branch instanceof DetachedAssociationCriteria<?> block && !block.getCriteria().isEmpty() ? |
There was a problem hiding this comment.
or { and { child { isNull('name') } }; eq('name', 'nobody') }, where orphan has no child. The list returns orphan. The child block is not the direct branch, so the missing child is not rejected. Require the child there too.
There was a problem hiding this comment.
@jamesfredley your findings from the 16:22 and 20:02 reviews:
or { and { child { isNull('name') } }; eq('name', 'nobody') }: the child is now required of any block with criteria inside anorwith more than one branch, a direct branch or nested in anand, soorphanis not returned. It is pinned inCriteriaOrAssociationBlockSpec, which failed before the change.- The shared cases skip Hibernate 5 for now: its binding on this branch still joins a block with an inner join. The same fix for it is fix(hibernate5): an association block that is a branch of or keeps the entities without the association #16931 on 7.0.x; when that is merged up, it removes the skip, as Neo4j: isNull and isNotNull on an association test the relationship #16836 will for Neo4j.
- The null-test case from the 16:22 review is
c7fc067e5e, which has its own case.
The full Hibernate 7 suite passes.
… entity, and the shared cases skip Hibernate 5 until its fix is merged up
The row of the left join is required of any block with criteria inside an or with more than one branch, not only of a direct
branch, so or { and { child { isNull('name') } }; eq('name', 'nobody') } no longer returns an entity without a child.
jamesfredley
left a comment
There was a problem hiding this comment.
A null test nested in an or no longer matches an entity that has no association. Not approving. An explicit left join no longer matches a missing association, and the shared case still fails on a datastore that has not changed.
| } | ||
|
|
||
| then: | ||
| names(found) == ['orphan', 'with'] |
There was a problem hiding this comment.
or { eq('child', kid); eq('name', 'orphan') } returns only with on a datastore that still requires the association for the comparison. Do not require orphan from a datastore that has not changed.
…anner chose for an or, not to one the caller joined, and one shared case skips Hibernate 5 A caller that joins the association with a left join keeps the entities without it in the or; the TCK case that compares an association to an instance in an or skips Hibernate 5 until its fix is merged up.
jamesfredley
left a comment
There was a problem hiding this comment.
An explicit left join matches a missing association again. Thread PRRT_kwDOAAfRJ862b-EZ resolved. Not approving. A child with a composite id still matches when it is missing.
| */ | ||
| private Predicate requireAssociated(JpaQueryContext context, String path, From<?, ?> joined, PersistentProperty<?> identity, | ||
| Predicate predicate) { | ||
| if (alternativesDepth > 0 && identity != null && context.isLeftForAlternatives(path) && |
There was a problem hiding this comment.
or { child { isNull('name') }; eq('name', 'nobody') }, where orphan has no child and the child's id is composite. The list returns orphan. The existence check is skipped because there is no single id. Require the child there too.
…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
| if (configured != null) { | ||
| return configured; | ||
| } | ||
| if (restricts && disjunctions > 0) { |
There was a problem hiding this comment.
with has a child named kid. orphan has no child. or { child(0) { eq('name', 'kid') }; eq('name', 'orphan') } should return only with, because the caller asked for an inner join. It returns orphan and with. The numeric join argument is not recorded, so this treats the join as unconfigured and makes it a left join. Keep an explicit inner join.
…n entity with a composite id is required too
A block inside an or { } is built by the detached criteria, which read only a string alias from the
first argument, so child(0) { } lost its join type and the planner made it a left join: an entity
without the association matched. The block now joins the association with the type it names, as the
criteria builder does outside an or.
The existence check of a block that is a branch of an or took the identity property of the associated
entity, which an entity with a composite id does not have, so it was skipped. It uses the first id
property in that case.
Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley Both cases are fixed in the new commit, and my earlier answer on the join argument was wrong: it does reproduce.
A child with a composite id. The existence check took the identity property of the associated entity, which an entity with a composite id does not have, so the check was skipped. It uses the first id property of the composite id now. The spec has an entity with a two-property id, where Four of the six rows in that spec fail without the change. The Hibernate 7 module (4631 tests), the Hibernate 5 module, the in-memory store, If you find another shape that does not reproduce first time, a failing spec on a throwaway branch, or the few lines you ran, would help me a lot. |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. An explicit inner join inside or is kept. A later left join on the same path still excludes a child that has no grandchild.
| if (configured != null) { | ||
| return configured; | ||
| } | ||
| if (restricts && disjunctions > 0) { |
There was a problem hiding this comment.
An earlier block marks grandchild as a left join that still requires the row. A later join('grandchild', LEFT) should keep the children that have no grandchild. It drops them, because the earlier mark is kept. Drop that mark when the caller names the join.
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 was an inner join, so an account without transactions was dropped before the other branch could match it.A block that restricts the association, in an
orwith more than one branch, is now a left join. A block outside anor, a block without criteria and the only branch of anorkeep the inner join, asCreateAliasJoinSpecpins.createAliasis unchanged: the docs say it is an inner join unless a join type is given.SqlRestrictionHibernate7Specexpected the dropped row (['ABCDEF']) foror { children { sqlRestriction(...) }; eq('name', 'ABC') }; it now expects['ABC', 'ABCDEF'].Tests: new shared TCK specs
CriteriaOrAssociationSpec(instance,isNull,isNotNullinor: the same on every binding) andCriteriaOrAssociationBlockSpec(block and to-many block inor; MongoDB skipped, it has no association blocks);CriteriaVisitorSpeccovers the new junction callbacks. Full Hibernate 5 suite and the affected Hibernate 7 specs pass,codeStyleis clean.CriteriaOrAssociationBlockSpecalso runs on Neo4j and passes there with #16836, so merge #16836 first.Fixes #16924
Evidence and the doc passage: #16924 (comment)
https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL