Skip to content

fix(hibernate7): an association block that is a branch of or keeps the entities without the association - #16930

Open
borinquenkid wants to merge 7 commits into
8.0.xfrom
fix/h7-or-association-block-8.0.x
Open

borinquenkid wants to merge 7 commits into
8.0.xfrom
fix/h7-or-association-block-8.0.x

Conversation

@borinquenkid

Copy link
Copy Markdown
Member

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 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, as CreateAliasJoinSpec pins. createAlias is unchanged: the docs say it is an inner join unless a join type is given.

SqlRestrictionHibernate7Spec expected the dropped row (['ABCDEF']) for or { children { sqlRestriction(...) }; eq('name', 'ABC') }; it now expects ['ABC', 'ABCDEF'].

Tests: new shared TCK specs CriteriaOrAssociationSpec (instance, isNull, isNotNull in or: the same on every binding) and CriteriaOrAssociationBlockSpec (block and to-many block in or; MongoDB skipped, it has no association blocks); CriteriaVisitorSpec covers the new junction callbacks. Full Hibernate 5 suite and the affected Hibernate 7 specs pass, codeStyle is clean. CriteriaOrAssociationBlockSpec also 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

…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
Copilot AI balanced review requested due to automatic review settings November 11, 2026 15:15

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.

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' })

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.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. CriteriaOrAssociationBlockSpec is skipped on Neo4j now (and still on MongoDB), with the reason in its javadoc: Neo4j gets its fix in #16836, and that change removes the skip. Fixed in c7fc067.

…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 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.

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() ?

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.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jamesfredley your findings from the 16:22 and 20:02 reviews:

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

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']

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.

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

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) &&

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.

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.

@borinquenkid
borinquenkid requested a review from matrei November 12, 2026 14:12
…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

@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 inner join inside or becomes a left join, so an entity without the association is kept.

if (configured != null) {
return configured;
}
if (restricts && disjunctions > 0) {

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.

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
@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley Both cases are fixed in the new commit, and my earlier answer on the join argument was wrong: it does reproduce.

or { child(0) { eq('name', 'kid') }; eq('name', 'orphan') }. The block of an or { } is built by the detached criteria, which read only a string alias from the first argument of a block, so the join type was dropped before the planner saw it and the planner made a left join. A block now joins its association with the type its first argument names (0 inner, 1 left, 2 right), the way the criteria builder does outside an or, so only with comes back. CriteriaOrAssociationJoinTypeHibernate7Spec runs the inner join through createCriteria() and through a detached criteria, and has child(1) { isNull('name') } finding orphan.

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 orphan no longer matches.

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, grails-datamapping-core and codeStyle pass. I also fixed an indentation codeStyle flagged in CriteriaOrAssociationBlockSpec.

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 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 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) {

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.

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.

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.

An association block inside or { } drops the rows without the association on Hibernate 5 and 7

3 participants