Repository navigation
fix(hibernate5): sort maps, composite id sorts, empty in lists and unresolved hasMany sorts - #17008
borinquenkid wants to merge 4 commits into
Conversation
…pty in list and a hasMany sort that does not resolve
- criteria list(sort: [a: 'asc', b: 'desc']) cast the map to a String and threw a
ClassCastException; the map is applied with the other arguments.
- the JPA criteria sort called CriteriaQuery.orderBy once per key, and orderBy replaces
the order it had, so a static list(sort: map) sorted by the last key only. The orders
are now added up. The same method read entity.getIdentity().getName(), which is null
for a composite id, so sorting such an entity threw a NullPointerException.
- an empty list given to inList is an explicit false, so its negation matches every row
(not { inList('year', []) } and findAllByYearNotInList([]) matched nothing on
PostgreSQL, MySQL and MariaDB).
- a default sort of a hasMany that names a property the associated entity does not
have failed with a NullPointerException at startup; it now says which association
and which sort.
Fixes #14653
Fixes #14451
Fixes #14533
Fixes #14457
Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…sort mapping CriteriaSortAndListFormsSpec (14 sort forms and 10 list forms, on Hibernate 5, the in-memory store and MongoDB), and for Hibernate 5 CompositeIdSortHibernateSpec and HasManySortMappingHibernateSpec. Refs #14653 #14451 #14533 #14457 Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
| final String sort = (String) argMap.get(HibernateQueryConstants.ARGUMENT_SORT); | ||
| // a map of sort properties is applied with the other arguments, in populateArgumentsForCriteria | ||
| final Object sortArgument = argMap.get(HibernateQueryConstants.ARGUMENT_SORT); | ||
| final String sort = sortArgument instanceof Map ? null : (String) sortArgument; |
There was a problem hiding this comment.
list(sort: ['author.name': 'asc']) { author { eq('active', true) } } should return the matching books ordered by author name. It throws a duplicate association path for author. The map sort does not reuse the join the block already made. Reuse that alias.
| aliasMap.put(propertyName, alias); | ||
| } | ||
| addToCriteria(Restrictions.in(alias + ".elements", values == null ? Collections.EMPTY_LIST : values)); | ||
| addToCriteria(AbstractHibernateCriterionAdapter.inRestriction(alias + ".elements", values)); |
There was a problem hiding this comment.
A book with no tags, queried with not { inList('tags', []) }, should come back. Membership in an empty list is false, so its negation matches every row. This joins the collection first, so the book is gone before the negation can match it. Handle the empty list before the join.
There was a problem hiding this comment.
Both of James's points reproduce for me (duplicate association path on the map sort, and not { inList('tags', []) } dropping untagged rows).
A few more:
in(String, Object[])inAbstractHibernateCriteriaBuilder(~L1373) andin(String, List)inAbstractHibernateQuery(L450, L1275) still callRestrictions.indirectly, so they fall back to Hibernate's own empty handling:in (null)on Postgres, MySQL, Oracle, SQL Server and DB2. With a Postgres dialect,not { inList('title', [] as Object[]) }giveswhere not title in (null)and returns nothing.- The 10 list-form rows in
CriteriaSortAndListFormsSpecall pass with the pre-fix main code too, since H2's dialect acceptsin (). So nothing here actually pins the empty-in fix yet. H2 withMODE=PostgreSQLandPostgreSQL10Dialectreproduces it, so that could pin it without waiting for Testcontainers. - The "order calls in a criteria closure" row passes before the fix as well. The
addOrderchange actually fixeslist(sort: [a:..., b:...])keeping only the last key, so the description should say that. - Malformed map sorts aren't refused:
sort: [title: 'sideways']silently goes asc,sort: [title: 1]throws a rawClassCastException. Either validate it or drop that from the description. - Description says 14 sort forms, the spec has 11.
where { author.active == true }.list(sort: ['author.name': 'asc'])fails withcould not resolve property: author.name. Plainsort: 'author.name'fails the same way, so it's older than this PR, probably a separate issue.
| * An "in" restriction on the given values. A list with no values matches nothing, so it is an explicit false: an | ||
| * empty IN list is not valid SQL on every database, and the negation of one has to match every row. | ||
| */ | ||
| public static Criterion inRestriction(String propertyName, java.util.Collection<?> values) { |
There was a problem hiding this comment.
Only used from the same package, so this could be package-private instead of new public API. Also java.util.Collection could just be an import.
| List<String> byMap = CompUserRole.list(sort: [user: 'asc', role: 'desc']).collect { it.user.name + ':' + it.role.name } | ||
|
|
||
| then: 'sorting by an association sorts by its id' | ||
| byRole.size() == 3 |
There was a problem hiding this comment.
These two only check size, so they pass in any order. Should be byRole == ['admin', 'admin', 'user'] and byUserDesc == ['bob', 'ann', 'ann'].
| Long id | ||
| Long version | ||
| String name | ||
| Integer rank |
There was a problem hiding this comment.
RANK is reserved in MySQL 8.0.2+, so this will likely break once the Testcontainers spec runs it. Maybe position or tier.
…st is decided before any join, a bad sort direction is refused A map of sort properties ordered a property of an association through a second join even when the query's own block had joined it, which failed with a duplicate association path; it uses the block's alias now, in the order the map lists its properties. An empty in list on a collection of strings joined the collection first and so dropped the rows without elements before a negation could match them; the empty list is an explicit false before any join, and the array form of in and the query form of in go through the same rule as the list form. A sort direction that is not asc or desc is refused with a message instead of sorting ascending. CriteriaRanked renames rank to tier, a reserved word in MySQL 8. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley Both findings are fixed in the new commit, and Sanjana's review is answered below. Map sort and a block's join. A map of sort properties now orders a property of an association the query's block already joined through that join, in the order the map lists its properties, so Empty list on a collection of strings. The empty list is decided before the collection is joined, so Sanjana's points
Branch policy: A sort direction that is not asc or desc, and a hasMany sort mapping that names no property, now fail with a message where they sorted ascending or threw a NullPointerException, as the other sort forms already do; the binder change refuses that mapping and generates the same schema; the rest changes which rows a query returns. |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A map sort that joins an association the query already joined still fails when only part of the path is joined. An explicit sort map also brings back the mapping's default sort.
| final Object sortArgument = argMap.get(HibernateQueryConstants.ARGUMENT_SORT); | ||
| if (sortArgument instanceof Map) { | ||
| applyMapSort(argMap, (Map<?, ?>) sortArgument); | ||
| @SuppressWarnings("unchecked") Map argMap2 = new HashMap(argMap); |
There was a problem hiding this comment.
The mapping sorts by author.name. list(sort: [title: 'asc']) {} should not join author. Removing the sort map here lets the default sort run again, so a book with no author is dropped. A sort through author.publisher also joins author twice and the query fails. Keep the explicit sort, and reuse a join that already covers the start of the path.
|
I went through the new commit and reran my earlier checks. Everything I raised looks resolved except one point: a sort map with an invalid direction (for example |
…mapping off, and reuses the joins of the query
A map of sort properties, and a string sort on an association the query joined, were removed from the
arguments once applied, so the default sort of the mapping ran as well: a mapping that sorts by
author.name joined author again and failed, or dropped the books without an author. The sort is
kept as one without properties, which leaves the default off and adds no order.
A sort through a path of which the query joined only the start, as author.publisher.name after
author { }, joined author a second time and failed with a duplicate association path. The part of
the path the query joined is reused and the rest is joined from it, for a map and for a string sort.
Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley Both cases reproduced, and are fixed in the new commit.
How to get the old behaviour: there is none to opt into. The query did not run before (duplicate association path), or ran with the default sort added to the one asked for. The Hibernate 5 module (742 tests) and Branch policy: the new types and throws in this PR were already in it; this commit adds none. Where the PR belongs is still open; I have not moved it. 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 sort the call gives keeps the mapping's default sort off. A sort through an embedded property of an association the query already joined still fails.
| } | ||
| aliasCount++; | ||
| String alias = association.substring(lastDot + 1) + ALIAS + aliasCount; | ||
| createAlias(association, alias, org.hibernate.sql.JoinType.INNER_JOIN.getJoinTypeValue()); |
There was a problem hiding this comment.
author is already joined. list(sort: ['author.address.city': 'asc']) { author { isNotNull('name') } } should order by city. It throws, because address is embedded and this tries to join it. Order by the embedded property on the join that already exists.
|
Closing this as superseded, to split it by the dev@ rule and to put the fixes on 7.0.x, where the bugs are (this one sat on #17007's branch, so it could not be retargeted without carrying #17007's commits, and I do not force-push).
The commits are the same ones, cherry-picked; the direction check is the only code that moved between the two. |
Stacked on the finder PR (base is its branch; merge that one first). Hibernate 5 took these query forms wrongly or failed on them:
sortargument of criteria and detached criteria (several properties, each with its direction) is applied with every property, in the order it lists them:list(sort: [a: 'asc', b: 'desc'])kept only its last property. A property of an association the query's block already joined is ordered through that join instead of failing with a duplicate association path, and a direction that is notascordesc(or is a number or null) is refused with a message instead of sorting ascending or throwing aClassCastExceptioninwith an empty list matches nothing, and its negation matches every row, on every database (the emptyin ()was invalid SQL on some, and the negation was wrong on three). The array form ofin, the query form ofinand an empty list on a collection of strings (decided before the collection is joined) follow the same rulehasManysortmapping that does not resolve to a property fails with an error naming itCriteriaSortAndListFormsSpec(TCK: 11 sort forms and 10 list forms, run on Hibernate 5, the in-memory store and MongoDB, each form through criteria, detached criteria, where queries and dynamic finders),MapSortAndEmptyListHibernateSpec,EmptyInListPostgresDialectSpec(H2 in PostgreSQL mode with the PostgreSQL dialect, where Hibernate renders an empty list asin (null)),CompositeIdSortHibernateSpecandHasManySortMappingHibernateSpecpin them. The sort and list forms of the TCK spec also pass without the fix, since H2's own dialect acceptsin ()and the sort forms were already applied by those entry points; they guard the forms across datastores. A Testcontainers spec for the empty list on real databases needs #16957 and follows it.Full suites on this branch: Hibernate 5 737 tests, in-memory 595, MongoDB 594 (2 workers), 0 failures;
codeStyleclean.A
wherequery on an association cannot be sorted by a property of that association (also with a plainsort:): #17017, older than this pull request.Fixes #14653
Fixes #14451
Fixes #14457
Refs #14533
https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL