Repository navigation
fix(hibernate): fetch join of to-one associations and criteria over basic-value collections - #16846
borinquenkid wants to merge 11 commits into
Conversation
…st queries findAll(), list() and the criteria builder ignored 'fetch: join' on a to-one association, causing one extra select per row. H5 now adds a left fetch join in the JPA list query; H7 does so in the HQL list builder and in the JPA criteria creator. Associations without the mapping stay lazy. Fixes #14343 Claude-Session: https://claude.ai/code/session_01Dmki4SQGwRSj8pRJ5KCWyX
A criteria block named after a hasMany collection of enums or other basic values threw MissingMethodException. The block now restricts the values through the 'elements' property with eq and in, in both bindings. Fixes #14374 Claude-Session: https://claude.ai/code/session_01Dmki4SQGwRSj8pRJ5KCWyX
jamesfredley
left a comment
There was a problem hiding this comment.
Not approving. Two restrictions in one collection block match different elements, and a paged count fails when a to-one is fetched.
|
|
||
| public Object in(String property, Collection<?> values) { | ||
| requireElements(property); | ||
| return query.in(collection, new java.util.ArrayList<>(values)); |
There was a problem hiding this comment.
kinds { eq('elements', Email); eq('elements', Phone) } matches a contact that has those kinds on different elements. Both tests must be of the same element.
| if (property instanceof ToOne && !(property instanceof Embedded) && !explicitFetches.contains(property.getName()) && | ||
| property.getMapping() != null && property.getMapping().getMappedForm() instanceof PropertyConfig && | ||
| ((PropertyConfig) property.getMapping().getMappedForm()).getFetchMode() == FetchMode.JOIN) { | ||
| queryRoot.fetch(property.getName(), JoinType.LEFT); |
There was a problem hiding this comment.
An entity with a join-mapped to-one, then list(max: 10).totalCount, throws. The count still fetches the association. Count without that fetch.
…e element, and the count of a paged list does not fetch
kinds { eq('elements', Email); eq('elements', Phone) } matched a contact that has those kinds on
different elements on Hibernate 7: each restriction was a test of its own. Every restriction of the
block is a test of the same element, as it is on Hibernate 5, so the restrictions now narrow one set
of values and are applied once, after the block: no element is both, and an in and an eq find the
element that satisfies both.
On Hibernate 5, list(max: n).totalCount threw for an entity with a to-one mapped fetch: 'join'
('query specified join fetching, but the owner of the fetched association was not present in the
select list'): the count reused the criteria of the list, with the fetch of the root in it. The count
is made without the fetches of the root.
Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley Both cases reproduced, and are fixed in the new commit.
The Hibernate 5 module (1,171 tests) and the Hibernate 7 module (4,302 tests) pass, and so does Branch policy: the throw that is listed in 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. A block over basic values still drops a matching number when the two restrictions spell that number with different scales.
| allowed = new java.util.LinkedHashSet<>(values); | ||
| } | ||
| else { | ||
| allowed.retainAll(values); |
There was a problem hiding this comment.
A collection holds 1.00. values { eq('elements', 1.0); eq('elements', 1.00) } should return that entity. It returns nothing, because 1.0 and 1.00 are not equal as Java values. Compare them as the stored number, not as Java objects.
|
The Hibernate 5 half is now #17055 against 7.0.x: neither fix changes a result that worked, so they go to the oldest branch with the bug (dev@ rule). This PR keeps the Hibernate 7 half; once the 7.0.x PR is merged and merged up, the Hibernate 5 files drop out of the diff. |
|
@jdaugherty: fixes #14343 (open since 2023-08) and #14374 (open since 2022-02), in the Hibernate 5 and 7 bindings. Your read of the fetch-join and criteria handling would help. |
… a number with different scales apply to the same stored number
values { eq('elements', 1.0); eq('elements', 1.00) } narrowed the allowed set with retainAll, which compares Java values, and
1.0 and 1.00 are not equal as BigDecimal, so a collection that holds 1.00 matched nothing. The values are compared as the
numbers they store.
Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley Reproduced and fixed. The block narrowed its allowed values with |
…lement-collection-8.0.x # Conflicts: # grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/query/GrailsHibernateQueryUtils.java
… of the mapped fetch join Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jdaugherty
left a comment
There was a problem hiding this comment.
Reviewed at 6ff5cb0d1958675b18fc00ebf6b7075b6c99f6ea. The paged-count, same-element enum, and BigDecimal-scale fixes address the earlier reported cases. Two gaps remain.
1. Keep database equality for collection restrictions. CriteriaMethodInvoker.java:449-471 intersects nonnumeric values with Java equality before building SQL. For hasMany = [tags: String] under a case-insensitive collation, an element containing alpha satisfies both restrictions:
tags {
eq('elements', 'alpha')
eq('elements', 'ALPHA')
}The Java intersection instead becomes empty, so the query rejects the matching entity. Please translate the conjunction against one database element and add a public criteria regression using a case-insensitive collation, retaining the tests that prevent different elements satisfying different restrictions.
2. Honor an explicit SELECT fetch mode. With destination fetch: 'join' in the mapping, Trip.createCriteria().list { fetchMode('destination', org.hibernate.FetchMode.SELECT) } still gets a fetch join. HibernateCriteriaBuilder.java:591-595 records SELECT in the detached fetch strategies, but JpaCriteriaQueryCreator.java:141-148 checks only eager fetch paths before applying the mapping. The caller cannot override the mapped join. Honor explicit fetch strategies and add a public criteria test verifying SELECT produces no fetch join while preserving the result rows.
Verification: an H2 predicate-only probe with SET COLLATION ENGLISH STRENGTH PRIMARY matched one stored row against both string predicates. Both GORM findings are source-traced; I have not executed these GORM reproducers.
Five files conflicted. The imports and the field of the spec keep both sides. The count of a paged list in Hibernate 5 takes the version of 8.0.x, which handles a fetch of a collection and a sort through a fetched association. In Hibernate 7 the mapped fetch join of a to-one association keeps its place next to the fetches that 8.0.x handles: the list applies it as a left fetch join and the criteria apply it as a fetch with the join type the query requests, as a requested fetch is, and a count has none because it has no entity to fetch into. The count of the HQL list is the one of 8.0.x. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
Merged 8.0.x to clear the conflicts; the merge changes nothing beyond resolving them.
Both Hibernate modules compile and pass: Hibernate 5 core (3850 tests) and Hibernate 7 core (5422 tests), and |
…lement-collection-8.0.x
…sts of one element, compared by the column, and an explicit select overrides a mapped fetch join
- The restrictions of a block over a collection of basic values, tags { eq('elements', 'alpha'); eq('elements', 'ALPHA') }, were narrowed
to the values they share as Java strings, so on a database whose collation takes the two for equal the entity that has the element
was refused. A block of several restrictions is now a correlated subquery over the elements of the collection, whose element is one
of the values of each restriction (ElementsInAll), so the database compares the column with the values: it applies the collation of
the column and compares numbers as the numbers they store, and the Java narrowing of the values is gone. Restrictions that need
different elements still do not satisfy each other.
- A to-one association that the mapping fetches with a join was fetch-joined also when the call names it with an explicit select, as
fetchMode('destination', FetchMode.SELECT) does, so the caller could not override the mapping. An explicit select is honoured, with
the rows unchanged.
|
Pushed the next commits, which answer the review of 12-03 at 6ff5cb0. Both gaps are in Hibernate 7 and both are fixed; I also merged 8.0.x first (no conflict). 1. Database equality for the restrictions of a block over basic values. You are right:
2. An explicit SELECT fetch mode. Two things the table showed and I left alone:
|
Two query fixes for long-open issues, in both the Hibernate 5 and Hibernate 7 bindings. Each has unit specs in both core modules, a scenario in both example apps, and a docs note in
createCriteria.fetch: 'join'on OneToOne association results in n+1 queries #14343 A to-one association mapped withfetch: 'join'is now loaded in the same select byfindAll(),list()and criteria queries, so reading it for each row no longer costs one extra query per row. An association without the mapping stays lazy, and an explicitfetch:argument is not repeated.hasManycollection of enums or other basic values no longer throwsMissingMethodException. The block restricts the values throughelementswitheqandin, for examplekinds { eq('elements', ContactKind.EMAIL) }.On Hibernate 7, an explicit select of the caller (
fetchMode('destination', FetchMode.SELECT)of the criteria builder, orfetch: [destination: 'select']oflist) overrides the mapped join, and several restrictions in one block over basic values are tests of one element, compared by the column with its collation (ElementsInAll, an internal class of the module).Branch policy:
ElementsInAllis an internal class of the Hibernate 7 module, public only because the criteria builder and the predicate generator are in different packages, and the throw is the refusal of a property other thanelementsin a block over basic values, which this pull request adds with the block and which Hibernate 5 also refuses.Fixes #14343
Fixes #14374
https://claude.ai/code/session_01Dmki4SQGwRSj8pRJ5KCWyX