Repository navigation
fix(hibernate5): map sorts, composite id sorts, empty in lists and the joins of a sort - #17052
borinquenkid wants to merge 17 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
…y list is decided before any join 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 direction that is not desc sorts ascending, as it always has. CriteriaRanked renames rank to tier, a reserved word in MySQL 8. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…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
…ist changes under 7.0.18 Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A map sort of two properties on the same association still fails when the query has not already joined that association.
| for (Map.Entry<?, ?> entry : sortMap.entrySet()) { | ||
| String property = (String) entry.getKey(); | ||
| String order = GrailsHibernateQueryUtils.sortDirection(property, entry.getValue()); | ||
| if (!addOrderOnJoinedPath(entity, property, order, ignoreCase)) { |
There was a problem hiding this comment.
list(sort: ['author.name': 'asc', 'author.active': 'desc']) should return the books sorted by name, then by active. It throws, because each property joins author again. Remember the join the first property made, and use it for the second.
7.0.18 is staged without these changes, so the note belongs to the next patch release. The sentence about keeping the earlier result no longer reads as a way to recover deleted rows. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A map sort of two properties on the same association still fails when the query has not already joined that association.
| for (Map.Entry<?, ?> entry : sortMap.entrySet()) { | ||
| String property = (String) entry.getKey(); | ||
| String order = GrailsHibernateQueryUtils.sortDirection(property, entry.getValue()); | ||
| if (!addOrderOnJoinedPath(entity, property, order, ignoreCase)) { |
There was a problem hiding this comment.
list(sort: ['author.name': 'asc', 'author.active': 'desc']) should return the books sorted by name, then by active. It throws, because each property joins author again. Remember the join the first property made, and use it for the second.
…s it once list(sort: ['author.name': 'asc', 'author.active': 'desc']) joined author once for each property, which failed with a duplicate association path when the query had not joined it already. An association that two or more of the sort properties go through is joined once, and each of them orders by that join. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley: |
… an empty list Dropping the criterion returns every row, not none; the note for the sort and the empty list says so. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley: the upgrade note no longer tells a reader to remove the criterion of an empty |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A map sort of two properties on the same association still fails.
| for (Object key : sortMap.keySet()) { | ||
| String property = (String) key; | ||
| int lastDot = property.lastIndexOf('.'); | ||
| if (lastDot > 0) { |
There was a problem hiding this comment.
list(sort: ['author.name': 'asc', 'author.publisher.name': 'desc']) should return the books sorted by name, then by the publisher. It throws, because each property joins author again. Remember the join the first property made, and use it for the second.
…re query, joins each association once Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley: Before asking for another look, I ran six sort shapes (two properties of one association, a nested path in either order, three levels with a plain property, a nested path first) against eight entry points: a criteria list with no block, with a block on the author, with a block on the author and its publisher, the paged list, a where query, a paged where query, |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A map sort through a nested association still joins the first association twice.
| } | ||
| 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.
list(sort: ['author.name': 'asc', 'author.publisher.name': 'desc']) should return the books sorted by name, then by the publisher. It throws, because the first join is not remembered and author is joined again. Remember that join, and use it for the second property.
…ion of the query joined orders by that join Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…on its condition joined is sorted, with a note in the guide Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley: Before asking for another look, I ran the nested sort over 17 contexts (a block on the author, an empty block, an or over two blocks, a block down to the publisher, an alias or an inner or left join made by the closure, a fetch, paging, ignoreCase off, an order argument as well, a where query with a condition on the author or on the publisher or with nested blocks, findAll, a dynamic finder, a second page), checking the size of each result and that it is in order, plus seven string and map sorts on queries whose condition already joined the author. 82 rows in the spec, 11 fail without this change. An old spec of issue 9860 expected that sort to throw because no alias was named; it now expects the sorted result, and the guide has a line for it. |
…as not been staged again Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@matrei: the notes are back under |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A map sort on a criteria builder that was constructed on its own throws.
| if (caseArg instanceof Boolean) { | ||
| ignoreCase = (Boolean) caseArg; | ||
| } | ||
| PersistentEntity entity = datastore.getMappingContext().getPersistentEntity(targetClass.getName()); |
There was a problem hiding this comment.
A criteria builder constructed with the class and the session factory, then listed with a sort map, should return the rows. It throws, because this asks for a datastore that was not given. Sort a property of the class without that datastore.
… a datastore sorts the properties of the class Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
A builder constructed with the class and the session factory now lists with a sort map: the map is left to the same sort code the other paths use, so A dotted association path in the map ( |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A map sort of two properties on the same association still fails on the older criteria path.
| for (Object sort : sortMap.keySet()) { | ||
| final String order = DynamicFinder.ORDER_DESC.equalsIgnoreCase((String) sortMap.get(sort)) ? DynamicFinder.ORDER_DESC : DynamicFinder.ORDER_ASC; | ||
| addOrderPossiblyNested(c, entity, (String) sort, order, ignoreCase); | ||
| addOrderPossiblyNested(c, entity, (String) sort, sortDirection(sort, sortMap.get(sort)), ignoreCase); |
There was a problem hiding this comment.
A sort map of author.name and author.active given to a plain criteria should return the books. It throws, because each key joins author again. Join author once and order both properties through it.
… required for every match Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
The upgrade guide in this PR gave the same advice for an empty negated list, so I corrected it the same way, after your finding on #17082: return an empty list only where the negated list is required for every match, and leave that side out when it is one side of an |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A map sort that goes through an embedded value of a shared association throws.
| return false; | ||
| } | ||
| } | ||
| addOrder(criteria, aliasForSortedAssociation(association) + "." + property.substring(lastPropertyPos + 1), order, ignoreCase); |
There was a problem hiding this comment.
list(sort: ['author.name': 'asc', 'author.address.city': 'asc']) { } should return the books, where address is embedded. It throws, because this tries to join author.address. A sort on author.address.city alone works. Join only the association, and sort by address.city on its join. The earlier finding on the older criteria path still stands.
…iation orders by it on the join of the association Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley Two of your sort cases, on this head: a sort map with The static |
Fixes #14653
Fixes #14451
Fixes #14533
Fixes #14457
Supersedes #17008, which mixed fixes of different kinds and sat on the branch of #17007. This is its fixes for failing calls and wrong rows, on 7.0.x, where the bugs are. The one change that makes a working call throw is split into its own PR on top of this one.
Which part of the dev@ rule this is
Each old result contradicts what the call asks for (a failure, or rows that the call's own arguments rule out), so it goes to the oldest branch that has the bug and is merged up. Each is listed with how to get the earlier rows in the 7.0.18 section of the upgrade guide.
What changes on Hibernate 5
sortargument of a criteria or detached criteria threw aClassCastException; the map is applied, every property in the order the map lists them. The staticlist(sort: map)kept only its last property; it orders by all of them (this one changes the order of rows a call returns today).NullPointerException.inorinList, in each of its forms, is an explicit false, so its negation matches every row (it matched none on PostgreSQL, MySQL and MariaDB).sortof ahasManythat names a property the associated entity does not have failed with aNullPointerExceptionat startup; the error names the association and the sort.Tests
CriteriaSortAndListFormsSpec(TCK: 11 sort forms and 10 list forms, each through criteria, detached criteria, where queries and dynamic finders),MapSortAndEmptyListHibernateSpec,EmptyInListPostgresDialectSpec,CompositeIdSortHibernateSpec,HasManySortMappingHibernateSpecandExplicitSortKeepsDefaultSortOffHibernateSpec. 19 of the 48 rows of these six specs fail on the unfixed 7.0.x code.grails-data-hibernate5-core(734) andgrails-datamapping-core(252) pass, andcodeStyleis clean. CI is not run here (no ci).https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL