Repository navigation
fix(hibernate7): a junction without criteria adds nothing to the query - #17010
borinquenkid wants to merge 17 commits into
Conversation
An empty disjunction became criteriaBuilder.or(), which JPA defines as false, so
an 'or { }' beside a property returned no rows. A junction with no predicates
now produces none, as on Hibernate 5.
Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
… note in the docs Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
Answers the review of #17010: an empty junction was dropped, so under a negation the negation vanished (not { and {} } matched every row) and in an or the branch vanished. Hibernate 5, the reference, treats an empty junction as true: it adds no restriction beside a property, makes the or it stands in true, is neutral in an and, and makes a negation false; an empty not {} is an error. The shared table CriteriaEmptyJunctionSpec has 52 shapes (alone, beside a property, nested, under a negation, in an association block, and in each query form: list, count, get, paged and ordered, projected, detached, in a subquery) with Hibernate 5's results as the expected values; 22 fail on Hibernate 7 without the change. Each datastore skips the rows it does not answer yet, by suite property. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
Answers the review. An empty junction is no longer dropped: it is a true predicate wherever it stands, as it is on Hibernate 5, the reference. It adds no restriction beside a property, makes the
Where Hibernate 5 and a literal reading of the review differ: the review says to drop an empty disjunction. On Hibernate 5 an empty junction is true everywhere, so Not covered: a |
jamesfredley
left a comment
There was a problem hiding this comment.
The items this review found have been addressed. An empty conjunction inside a negation matches nothing, and a junction that has criteria still adds them.
…ls it, and a negation with nothing to negate is refused
not { parent {} } failed to parse: the block adds a match and an empty expression, which the negation wrapped as NOT (). A
negation with nothing left to negate now throws the same IllegalArgumentException an empty not {} does, as Hibernate 5 does.
not { child { or {} } } returned the node without a child, because the block was an existence check under a NOT. A block
with criteria is a join on Hibernate 5, so its negation is a neighbour that fails the criteria and a node without a
neighbour is not in it. The negation is now the conjunction of the negations of its criteria, with such a block as an
existence check of a neighbour that fails its criteria.
CriteriaEmptyJunctionSpec gains three rows (not of a block with an and, with nothing, with a property) and Neo4j no longer
skips not { child { or {} } }. H5, H7 with the fix of #17010 and Neo4j agree on all of them.
Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…x' into fix/h7-empty-junction-and-shapes-8.0.x # Conflicts: # grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/CriteriaOrAssociationBlockSpec.groovy
…n-and-shapes-8.0.x
…x' into fix/h7-empty-junction-and-shapes-8.0.x
…ntities without the association are left out
or { or { child {} }; eq('name', x) } is every entity that has a child on Hibernate 5, the reference, measured on 7.0.x: the inner or
holds only a block without criteria, which is true wherever it nests, and the block still asks for an associated entity. Six rows pin
that for or inside or, and inside or inside or, or inside and, and and inside or.
Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. An empty or inside a child block matches every owner that has a child.
| ['not { or {}; eq name=orphan }', { -> not { or { }; eq('name', 'orphan') } }, [], ['simple', 'neo4j']], | ||
| ['not { and {}; eq name=orphan }', { -> not { and { }; eq('name', 'orphan') } }, [], ['simple', 'neo4j']], | ||
| ['not { child { or {} } }', { -> not { child { or { } } } }, [], ['simple', 'neo4j', 'mongodb']], | ||
| ['child { or { or {}; eq name=kid } }', { -> child { or { or { }; eq('name', 'kid') } } }, ['other', 'with'], ['simple', 'neo4j', 'mongodb']], |
There was a problem hiding this comment.
child { or { or {}; eq('name', 'kid') } } should match only the owner whose child is named kid. This expects the other owner too, because the empty inner or is treated as true. An empty or matches nothing.
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. An empty or inside a child block still matches every owner that has a child.
| ['not { or {}; eq name=orphan }', { -> not { or { }; eq('name', 'orphan') } }, [], ['simple', 'neo4j']], | ||
| ['not { and {}; eq name=orphan }', { -> not { and { }; eq('name', 'orphan') } }, [], ['simple', 'neo4j']], | ||
| ['not { child { or {} } }', { -> not { child { or { } } } }, [], ['simple', 'neo4j', 'mongodb']], | ||
| ['child { or { or {}; eq name=kid } }', { -> child { or { or { }; eq('name', 'kid') } } }, ['other', 'with'], ['simple', 'neo4j', 'mongodb']], |
There was a problem hiding this comment.
child { or { or {}; eq('name', 'kid') } } should match only the owner whose child is named kid. This still expects the other owner too, because the empty inner or is treated as true. An empty or matches nothing.
There was a problem hiding this comment.
This is the same row as my reply above: Hibernate 5 returns both owners for it today, and the spec pins that shipped value. Making an empty or match nothing changes results for queries that already run, so I opened #17174 (10.0.0-M1) with the options. This PR keeps the shipped value until that is decided.
|
@jamesfredley The meaning of an empty
Hibernate 5 ships the first, and which one is right for every store is #17174 (10.0.0-M1), with the options. This pull request is the Hibernate 7 side of the first reading, so it should follow that decision. It is stacked on the contract pull request now, and its own diff is 4 files: it showed 236 before because its base branch was 276 commits behind 8.0.x, and that is merged. |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. An empty or inside a child block still matches every owner that has a child.
| // an empty junction is true wherever it stands, as it is on Hibernate 5: beside a property it adds no restriction, in an | ||
| // or it makes the or true, in an and it is neutral, and under a not the negation is false. Dropping it, or letting JPA | ||
| // make an empty disjunction false, answers differently in an or and under a not. | ||
| return criteriaBuilder.conjunction(); |
There was a problem hiding this comment.
child { or { or {}; eq('name', 'kid') } } should match only the owner whose child is named kid. It also matches the other owner, because the empty inner or is treated as true. An empty or matches nothing.
|
@jamesfredley My comment of 11-26 was not exact. I wrote that this pull request drops an empty disjunction. Beside a property it adds nothing, but inside an |
…ty-junction-and-shapes-8.0.x
…ty-junction-and-shapes-8.0.x
…nAssociationInOr Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. An empty or inside a child block still matches every owner that has a child.
| ['not { or {}; eq name=orphan }', { -> not { or { }; eq('name', 'orphan') } }, [], ['simple', 'neo4j']], | ||
| ['not { and {}; eq name=orphan }', { -> not { and { }; eq('name', 'orphan') } }, [], ['simple', 'neo4j']], | ||
| ['not { child { or {} } }', { -> not { child { or { } } } }, [], ['simple', 'neo4j', 'mongodb']], | ||
| ['child { or { or {}; eq name=kid } }', { -> child { or { or { }; eq('name', 'kid') } } }, ['other', 'with'], ['simple', 'neo4j', 'mongodb']], |
There was a problem hiding this comment.
child { or { or {}; eq('name', 'kid') } } should match only the owner whose child is named kid. This expects the other owner too. An empty or matches nothing. Expect only the owner whose child is named kid.
There was a problem hiding this comment.
@jamesfredley This pull request makes an empty junction true wherever it stands on Hibernate 7, which is the value Hibernate 5 ships: or { } beside a property returns the rows of that property alone. Your example expects an empty or to match nothing. That would change what Hibernate 5 returns for queries that run today, so what an empty or or and means is a decision for every store, and the options are in #17174. I left this pull request on the shipped value.
jdaugherty
left a comment
There was a problem hiding this comment.
Reviewed the four-file diff against the stacked base at e322cda. One Hibernate 5 compatibility gap remains: the new empty-negation exception also rejects DetachedCriteria queries that Hibernate 5 executes successfully.
The empty-or results in CriteriaEmptyJunctionSpec agree with Hibernate 5 in the cases exercised; the alternative semantics remain the separate decision in #17174.
Validation on JDK 21 with H2: all 58 CriteriaEmptyJunctionSpec cases, both ReportedCriteriaShapesHibernateSpec cases, and all 8 CriteriaOrAssociationBlockSpec cases passed on Hibernate 7. The shared empty-junction spec passed on Hibernate 5 with its 3 declared skips. An additional public-API regression probe for detached empty negation passed on Hibernate 5 for list, count, and an inList subquery, but all three cases failed on Hibernate 7 at the new IllegalArgumentException. The full module suites were not run.
| if (predicates.length == 0) { | ||
| if (junction instanceof Query.Negation) { | ||
| throw new IllegalArgumentException("Logical expression [not] must contain at least 1 expression"); |
There was a problem hiding this comment.
[P2] Preserve the detached-criteria behavior of an empty negation
Hibernate 5 rejects createCriteria().list { not {} }, but its detached-criteria path accepts new DetachedCriteria(TestEntity).build { not {} }: list() returns [], count() returns 0, and using its ID projection in an inList subquery returns []. I verified all three through the public APIs on both backends; this new check makes all three throw IllegalArgumentException on Hibernate 7. Applications composing a detached negation from a dynamically empty set of conditions therefore fail instead of retaining the shipped Hibernate 5 result. The new TCK row tests the builder form only, so it misses this distinction. Keep the builder-specific validation at that boundary and preserve the false predicate for an empty detached negation, with regression coverage for the detached query forms.
There was a problem hiding this comment.
Fixed in c9003d0. The refusal of an empty not {} now lives where the criteria builder starts its query (CriteriaMethodInvoker, which walks the builder's own criteria, including or, and and association blocks). It no longer sits in the shared PredicateGenerator, which a DetachedCriteria also reaches. There an empty negation is a false predicate, so a detached not {} returns no rows, as on Hibernate 5.
CriteriaEmptyJunctionSpec has new rows for a detached not {} (list, count, and an inList subquery), plus builder rows that must still throw (or { not {} }, and { not {}; eq ... }, child { not {} }). Before the change the three detached rows failed on Hibernate 7 and passed on Hibernate 5. They pass on both now. The Hibernate 5 run of the shared spec has 64 passes and the 3 declared skips. The full Hibernate 7 core suite passes (4896 tests).
I did not run the new rows on MongoDB, so they are skipped there.
jdaugherty
left a comment
There was a problem hiding this comment.
Rechecked the four-file diff at dc9d99b against its stacked base. The main empty-junction change and the reported criteria-shape tests are consistent with the stated Hibernate 5 compatibility goal. No additional implementation bugs found.
The existing detached empty-negation finding remains applicable: #17010 (comment). The new exception still rejects detached not {} queries that Hibernate 5 executes with an empty result.
Fresh validation on JDK 21 with H2: all 58 CriteriaEmptyJunctionSpec cases and both ReportedCriteriaShapesHibernateSpec cases passed. These tests do not cover detached empty negation. Full module suites were not run.
…nly; a detached criteria runs it as false
Hibernate 5 throws for an empty not {} in the criteria builder and executes it in a
DetachedCriteria as a negation that matches nothing. The check moves from the shared
PredicateGenerator to the builder entry (CriteriaMethodInvoker), and the generator
returns a false predicate for an empty negation. The shared spec gains the detached
rows (list, count, inList id) and nested builder rows (or, and, child).
Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. Rejecting an empty not {} leaves the builder session open, and a later list() on the same builder does not reject it.
| private static void rejectEmptyNegation(List<Query.Criterion> criteria) { | ||
| for (Query.Criterion criterion : criteria) { | ||
| if (criterion instanceof Query.Negation negation && negation.getCriteria().isEmpty()) { | ||
| throw new IllegalArgumentException("Logical expression [not] must contain at least 1 expression"); |
There was a problem hiding this comment.
Book.createCriteria().list { not {} } with no bound session throws before the builder closes the session it opened. The session stays open and bound, so a later builder on that thread can reuse it. Close that session on this failure.
| } else { | ||
| invokeClosureNode(args[0]); | ||
| } | ||
| rejectEmptyNegation(hibernateQuery.getDetachedCriteria().getCriteria()); |
There was a problem hiding this comment.
def c = Book.createCriteria(); c.not {}; c.list() returns an empty list instead of rejecting the empty negation. The no-argument list() does not run the new check. c.singleResult() returns null the same way.
…he criteria builder, and the session the builder opened is closed when it is
A builder that was refused for an empty negation left the session it opened bound to the thread, so a later builder on the thread
reused it; and c.not {}; c.list() and c.not {}; c.singleResult() ran the query without the check, so they returned an empty list or
null where the closure forms are refused. The check is a method of the builder that closes the session as throwRuntimeException does,
and list(), singleResult() and the calls that take a closure all ask it first.
|
Pushed the next commit, which answers the 12-04 review: A session that stays open.
The class, not the two samples. One thing the table showed, which is not changed here:
|
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A fresh builder now closes the session and rejects the empty not {}. A reused builder still keeps the rejected negation, so the next query on that builder fails.
| } | ||
|
|
||
| public List<?> list() { | ||
| rejectEmptyNegation(); |
There was a problem hiding this comment.
In a caller-owned session, c.not {}; c.list() throws before the builder clears the criteria. Catch that exception and run c.list { eq('name', 'kept') } on the same builder. The second query throws because the rejected negation is still there. Clear the criteria when the rejection throws. singleResult() has the same gap.
…pty negation that it refused
c.not {}; c.list() threw before the builder reset its state, so with the builder kept by the caller, a session of the caller that stays open, the next query on
that builder, c.list { eq('name', 'kept') }, threw the same error because the refused negation was still in its criteria; singleResult() had the same gap. The
refusal now resets the query as a call that ran does, before it throws. CriteriaEmptyNegationSessionSpec has a table over the six positions of the empty
negation, the two calls that refuse it and the three queries that follow on the same builder, 36 rows; with the previous builder they fail.
|
Pushed the next commit, which answers the review of 12-05 at eb292a6. Your reproduction was right: in a caller-owned session, The refusal now resets the query, as a call that ran does, before it throws. It is one line in
|
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. The rejected negation is now dropped. The next query on a builder that opened its own session still has no session.
| // the refused query is dropped with the criteria it holds, as the query of a call that ran is, so that a builder that the | ||
| // caller keeps runs its next query without the negation that was refused | ||
| resetQueryState(); | ||
| throwRuntimeException(new IllegalArgumentException("Logical expression [not] must contain at least 1 expression")); |
There was a problem hiding this comment.
Outside a transaction, c.not {}; c.list() throws, and the catch closes the session this builder opened. The next call, c.list { eq('title', 'kept') }, throws No Session found for current thread instead of returning the matching rows. Open a session for that next call, or do not close the one the builder still needs.
Stacked on #17188 (base is its branch), which is stacked on #16930: merge #16930, then that one, then this one.
or { }becamecriteriaBuilder.or(), which JPA defines as false, soor { }beside a property returned no rows. It now produces no predicate, as on Hibernate 5.ReportedCriteriaShapesHibernateSpec(Hibernate 7) pins the criteria shapes users reported, and the Hibernate 7 where-queries guide shows how a where query asks whether a collection holds an instance (pets { id == petId }).grails-data-hibernate7core (4,786 tests),grails-datamapping-core(1,831) andgrails-datamapping-core-test(761, where the in-memory store runs the shared specs) pass on this branch, and so doescodeStyle. With the unchangedPredicateGenerator, 35 tests ofCriteriaEmptyJunctionSpecandReportedCriteriaShapesHibernateSpecfail.Refs #14573
Refs #14635
Refs #14617
What an empty junction means
This pull request implements the shipped Hibernate 5 value for Hibernate 7: an empty junction is true wherever it stands, so
or { }beside a property no longer returns no rows. A review asks for the opposite for an emptyor, an emptyorthat matches nothing, which changes what Hibernate 5 returns for queries that run today; that is a decision for every store, #17174, and the options are there.https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL