Skip to content

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

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

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

Conversation

@borinquenkid

@borinquenkid borinquenkid commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

#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 NullPointerException becomes 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 in list 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

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

CriteriaSortFormsSpec (TCK: 11 sort forms through criteria, detached criteria, where queries and dynamic finders), MapSortHibernateSpec, CompositeIdSortHibernateSpec, HasManySortMappingHibernateSpec and ExplicitSortKeepsDefaultSortOffHibernateSpec. With the main sources of 7.0.x, 44 of their 103 features fail. grails-datamapping-core (252), grails-datamapping-core-test (580) and grails-data-hibernate5-core (1475) pass, and so does codeStyle. 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.

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.

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

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

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

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

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

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

Copy link
Copy Markdown
Member Author

@jamesfredley I reproduced the two-path sort on the older criteria path: findAllWhere, findWhere, findAll(example) and find(example) threw duplicate association path: author when two sort keys went through one association. They do on plain 7.0.x too, but they take the map this PR adds, so I fixed them here: each association is joined once per sort map, also below an association (author.publisher.name with author.publisher.country). The new feature fails without the change and covers all four, plus a nested pair.

I also corrected the guide sentence on the empty negated list: inside an or that is itself under a not the result is the same as before, so that or is left as it is.

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

The items this review found have been addressed. A sort map joins each association once, including on the older criteria path.

@borinquenkid

Copy link
Copy Markdown
Member Author

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 CriteriaQuery path (static list) and the default sort of the mapping join it once too, which threw duplicate association path for two keys on one association. A spec covers the mapping sort.

One path is left: the deprecated GrailsHibernateUtil variant behind a standalone HibernateCriteriaBuilder. This PR does not touch it and it behaves the same on plain 7.0.x.

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

Copy link
Copy Markdown
Member Author

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

new HibernateCriteriaBuilder(Book, sessionFactory).list(sort: 'author.name') { } threw IllegalArgumentException: Unexpected: class is not a domain class, with a string sort and with a map sort alike. The workaround was Book.createCriteria(). The cause: a nested sort looked the property up in the GORM mapping (GrailsHibernateUtil.getGrailsDomainClassProperty), and such a builder has no datastore.

Without a datastore, the property is now looked up in the Hibernate metadata of the criteria's session factory:

  • an association is joined, reusing a join the criteria already has, from its block or from an earlier sort entry; this avoids duplicate association path;
  • a component (author.home.city) is sorted by its path;
  • anything else is sorted as given.

Tests and checks

  • A new MapSortAndEmptyListHibernateSpec feature covers a string sort, a map sort, a block that already joined author, two levels (author.publisher.name) and an embedded component, against the rows of Book.createCriteria(). It fails with the change reverted.
  • The Hibernate 5 module passes with 1089 tests, and codeStyle passes.
  • The upgrade note has a line for it.

This PR stays a draft until 7.0.x is merged into 7.2.x; then it moves to 7.2.x.

@jdaugherty jdaugherty 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.

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

Copy link
Copy Markdown
Member Author

@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 in lists, the hasMany default sort and keeping the default sort of the mapping off when the call gives a sort.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

… 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
@borinquenkid borinquenkid changed the title fix(hibernate5): map sorts, composite id sorts, empty in lists and the joins of a sort fix(hibernate5): map sorts, composite id sorts and the joins of a sort Nov 30, 2026
@jdaugherty jdaugherty mentioned this pull request Dec 1, 2026
9 of 17 tasks

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.

4 participants