Repository navigation
fix(hibernate5): map sorts, composite id sorts and the joins of a sort - #17052
borinquenkid wants to merge 23 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.
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.
…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.
…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.
… 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.
…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 |
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.
…rs that take an example or a map, and the empty negated list note leaves a negated or alone Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley I reproduced the two-path sort on the older criteria path: I also corrected the guide sentence on the empty negated list: inside an |
jamesfredley
left a comment
There was a problem hiding this comment.
The items this review found have been addressed. A sort map joins each association once, including on the older criteria path.
… an association once Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
James, I went through every place that applies a map or mapping sort, not only the one you named. The criteria builder and the finders that take an example or a map already joined an association once. Now the JPA One path is left: the deprecated |
The sort-map and join-once work of #17062-#17066 replaces this branch's own: the sort of a where query, the paged list and the JPA path now use 7.0.x's joins. Kept from this branch: the criteria-builder map sort, the direction of a map entry that is not a String, the composite id sort, the empty in list and the hasMany sort error. The upgrade note keeps only those. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…y sorts by a property of an association `new HibernateCriteriaBuilder(Book, sessionFactory).list(sort: 'author.name')`, with a string or a map, threw `IllegalArgumentException: Unexpected: class is not a domain class`: a nested sort looked the property up in the mapping, and such a builder has no datastore. Without one, the property is now looked up in the Hibernate metadata of the session factory of the criteria: an association is joined, reusing a join the criteria already has (from its block or an earlier sort entry), a component is sorted by its path, and any other property is sorted as given. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
Pushed 59b305e (merge of 7.0.x, which resolves the conflict with the merged #17062-#17066) and 7a0fff6, which fixes what this PR had left to a workaround. A sort by a property of an association on a criteria builder constructed with a session factory
Without a datastore, the property is now looked up in the Hibernate metadata of the criteria's session factory:
Tests and checks
This PR stays a draft until 7.0.x is merged into 7.2.x; then it moves to 7.2.x. |
jdaugherty
left a comment
There was a problem hiding this comment.
#17070 (part 10 of the #16853 split) overlaps this pull request in the paginationEnabledList branch of AbstractHibernateCriteriaBuilder and in GrailsHibernateUtil.addOrderPossiblyNested. It also fixes a map as the sort argument of a paged criteria query, which threw a ClassCastException, and makes the sort argument reuse a join that the criteria already have for the path, also one made through an alias, instead of joining it a second time. #17071 and #17072 then change the sort of the static list in GrailsHibernateQueryUtils for the same sorts.
Whichever of these lands second should keep one implementation of the map sort and of the reused join. The composite id sort, the empty in lists, the hasMany default sort and keeping the default sort of the mapping off when the call gives a sort are not in any part of #16853.
…empty-list-7.0.x # Conflicts: # grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/query/AbstractHibernateCriteriaBuilder.java
|
@jdaugherty Agreed: one implementation of the map sort and of the reused join. #17062, which this waited for, has merged, and the description now says so. This stays a draft while #17070 is open. If it lands second, I will drop what #17070 to #17072 cover and keep the composite id sort, the empty |
… change An empty list given to `in` or `inList` behaves as on 7.0.x again. The change, its specs, the list forms of the TCK spec and its upgrade note are removed; the sort fixes are unchanged. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
#17062, which this pull request waited for, has merged. It stays a draft while #17070 is open: the two overlap in the map sort of a paged criteria query and in the joins that a sort makes, so whichever lands second keeps one implementation.
Fixes #14653
Fixes #14451
Part of #14457: the
NullPointerExceptionbecomes an error that names the association and the sort. Sorting by a property of the related entity, which the issue also asks for, is not added.Supersedes #17008, which mixed fixes of different kinds and sat on the branch of #17007. This is its sort fixes, on 7.0.x, where the bugs are. The empty
inlist fix (#14533), which an earlier version of this pull request carried, is no longer here. 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.sortof ahasManythat names a property the associated entity does not have failed with aNullPointerExceptionat startup; the error names the association and the sort.Tests
CriteriaSortFormsSpec(TCK: 11 sort forms through criteria, detached criteria, where queries and dynamic finders),MapSortHibernateSpec,CompositeIdSortHibernateSpec,HasManySortMappingHibernateSpecandExplicitSortKeepsDefaultSortOffHibernateSpec. With the main sources of 7.0.x, 44 of their 103 features fail.grails-datamapping-core(252),grails-datamapping-core-test(580) andgrails-data-hibernate5-core(1475) pass, and so doescodeStyle. CI is not run here (no ci).https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL