Skip to content

fix(hibernate5): map sorts, composite id sorts, empty in lists and the joins of a sort - #17052

Open
borinquenkid wants to merge 17 commits into
7.0.xfrom
fix/h5-map-sort-and-empty-list-7.0.x
Open

borinquenkid wants to merge 17 commits into
7.0.xfrom
fix/h5-map-sort-and-empty-list-7.0.x

Conversation

@borinquenkid

Copy link
Copy Markdown
Member

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

  • A map as the sort argument of a criteria or detached criteria threw a ClassCastException; the map is applied, every property in the order the map lists them. The static list(sort: map) kept only its last property; it orders by all of them (this one changes the order of rows a call returns today).
  • Sorting a domain class with a composite id by one of its id properties threw a NullPointerException.
  • A map sort, or a string sort on an association the query's block joined, no longer joins the association a second time (a duplicate association path failure), and a sort that the call gives keeps the default sort of the mapping off.
  • An empty list given to in or inList, in each of its forms, is an explicit false, so its negation matches every row (it matched none 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; 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, HasManySortMappingHibernateSpec and ExplicitSortKeepsDefaultSortOffHibernateSpec. 19 of the 48 rows of these six specs fail on the unfixed 7.0.x code. grails-data-hibernate5-core (734) and grails-datamapping-core (252) pass, and codeStyle is clean. CI is not run here (no ci).

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

…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
Copilot AI balanced review requested due to automatic review settings November 19, 2026 03:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley: list(sort: ['author.name': 'asc', 'author.active': 'desc']) joins author once now. An association that two or more of the sort properties go through is joined once, before the properties are applied, and each of them orders by that join; embedded properties and paths that are not associations are left as they were. MapSortAndEmptyListHibernateSpec has the static list and the criteria list with two properties of the same association, and the feature fails without the change.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

… 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
@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley: the upgrade note no longer tells a reader to remove the criterion of an empty not in list, which returns every row, not none. To keep the earlier result, return an empty list before the query when the input list is empty. Docs only; no code changed in this commit.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley: list(sort: ['author.name': 'asc', 'author.publisher.name': 'desc']) failed because the check for an association that two sort properties share only counted the direct owner of each property, so author was joined by one property and again by the other. Every association on the way to a property counts now, so author is joined once.

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, findAll with a closure and a dynamic finder, and compared each with the order of the same books sorted in memory. That found one more failure that you had not named: on a where query, a map sort of two properties of the same association failed with the same duplicate association path, because HibernateQuery.order joined the association once per property. It joins each association path once now, nested ones included. 58 rows, 28 fail without these two changes.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley: list(sort: ['author.name': 'asc', 'author.publisher.name': 'desc']) works when a condition of the query has already joined the author. That was the case my last matrix did not have: a where query or findAll with author.active == true and any sort by author.name, even a plain string sort, failed with could not resolve property: author.name. When a join already existed for the association, HibernateQuery.order ordered by the full property name on the root criteria, where the join's generated alias is not a name it can resolve. It now orders each association on the path by its alias: the one the query's condition made, else one made once for every property that goes through that association.

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.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@borinquenkid

Copy link
Copy Markdown
Member Author

@matrei: the notes are back under === Grails 7.0.18 in upgrading60x.adoc, as your correction asks; the 7.0.19 heading is gone. If 7.0.18 is staged again before this merges, I move them. Only the upgrade notes changed.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@borinquenkid

Copy link
Copy Markdown
Member Author

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 sort: [title: 'desc'], 'DESC' and two keys in either order sort as they do on the domain class. The spec states the expected order of each form, on both builders.

A dotted association path in the map ('author.name') is not sorted on a builder without a datastore: it has no mapping to resolve the association against. It fails with IllegalArgumentException (plain 7.0.x threw ClassCastException for any map sort). Workaround: use Book.createCriteria(), which has the datastore and shares the join between paths. Sorting through a standalone builder with association paths is too large for a 7.0.x patch release; I can look at it on 8.x.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@borinquenkid

Copy link
Copy Markdown
Member Author

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 or.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley Two of your sort cases, on this head: a sort map with author.name and author.active on the domain's criteria returns the books in order (the second path reuses the join). author.name with author.address.city did throw, because the embedded part was joined as if it were an association. That is fixed in this push: only the association is joined, and the embedded property is ordered on its join. Three orders of the properties are in the new feature; it fails without the change.

The static Book.list(sort: ...) form with an embedded path throws the same way on plain 7.0.x, so I filed it as #17115 and left it out. The standalone builder with a dotted path is still the limit I mentioned before.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants