Repository navigation
fix(hibernate): fetch join of to-one associations and criteria over basic-value collections - #16846
borinquenkid wants to merge 8 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
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) }.Fixes #14343
Fixes #14374
https://claude.ai/code/session_01Dmki4SQGwRSj8pRJ5KCWyX