Skip to content

fix(hibernate5): sort maps, composite id sorts, empty in lists and unresolved hasMany sorts - #17008

Closed
borinquenkid wants to merge 4 commits into
fix/finder-text-after-operator-7.0.xfrom
fix/h5-sort-and-list-forms-7.0.x
Closed

borinquenkid wants to merge 4 commits into
fix/finder-text-after-operator-7.0.xfrom
fix/h5-sort-and-list-forms-7.0.x

Conversation

@borinquenkid

@borinquenkid borinquenkid commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Stacked on the finder PR (base is its branch; merge that one first). Hibernate 5 took these query forms wrongly or failed on them:

  • a map as the sort argument 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 not asc or desc (or is a number or null) is refused with a message instead of sorting ascending or throwing a ClassCastException
  • sorting a domain with a composite id by one of the properties of its id no longer fails on the missing identity property
  • in with an empty list matches nothing, and its negation matches every row, on every database (the empty in () was invalid SQL on some, and the negation was wrong on three). The array form of in, the query form of in and an empty list on a collection of strings (decided before the collection is joined) follow the same rule
  • a hasMany sort mapping that does not resolve to a property fails with an error naming it

CriteriaSortAndListFormsSpec (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 as in (null)), CompositeIdSortHibernateSpec and HasManySortMappingHibernateSpec pin them. The sort and list forms of the TCK spec also pass without the fix, since H2's own dialect accepts in () 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; codeStyle clean.

A where query on an association cannot be sorted by a property of that association (also with a plain sort:): #17017, older than this pull request.

Fixes #14653
Fixes #14451
Fixes #14457
Refs #14533

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

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 that joins an association the query already joined fails. Negating an empty in list on a collection of strings drops the rows that should match.

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;

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

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

@sanjana2505006 sanjana2505006 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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[]) in AbstractHibernateCriteriaBuilder (~L1373) and in(String, List) in AbstractHibernateQuery (L450, L1275) still call Restrictions.in directly, 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[]) } gives where not title in (null) and returns nothing.
  • The 10 list-form rows in CriteriaSortAndListFormsSpec all pass with the pre-fix main code too, since H2's dialect accepts in (). So nothing here actually pins the empty-in fix yet. H2 with MODE=PostgreSQL and PostgreSQL10Dialect reproduces 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 addOrder change actually fixes list(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 raw ClassCastException. 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 with could not resolve property: author.name. Plain sort: '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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member Author

@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 list(sort: ['author.name': 'asc']) { author { eq('active', true) } } returns the matching books ordered by author name. MapSortAndEmptyListHibernateSpec has your case, the joined property before and after a plain one, and a map sort with no block of its own (it still joins). The first two fail before the change.

Empty list on a collection of strings. The empty list is decided before the collection is joined, so not { inList('tags', []) } returns the book with no tags. It fails before the change.

Sanjana's points

  • in(String, Object[]) and in(String, List) of AbstractHibernateQuery passed an empty list to Hibernate unchanged: they use the same rule as the list form now (the array form through the list form). EmptyInListPostgresDialectSpec runs H2 in PostgreSQL mode with the PostgreSQL dialect, where Hibernate renders an empty list as in (null); its array row fails before the change. The other rows there (list, where, detached, finder) already passed, so they guard those paths and do not reproduce the failure.
  • A map sort whose direction is not asc or desc, a number or null is refused with a message naming the property; sort: [title: 'sideways'] no longer sorts ascending, and [title: 1] no longer throws a ClassCastException.
  • CompositeIdSortHibernateSpec checks the exact order. CriteriaRanked has tier instead of rank, a reserved word in MySQL 8. inRestriction is package-private. The pull request description now counts the forms correctly and says what the addOrder change fixes.
  • where { author.active == true }.list(sort: 'author.name') fails on plain sort: 'author.name' too, so it is older: Hibernate 5: a where query on an association cannot be sorted by a property of that association #17017.

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

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

@sanjana2505006

Copy link
Copy Markdown
Member

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 sort: [title: 'sideways']) is now refused through createCriteria().list and the static list, but a dynamic finder, a where query, and detached criteria still sort it ascending without an error. The description mentions detached criteria as refusing it, so it might be worth either extending the check to those paths or adjusting the description.

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

Copy link
Copy Markdown
Member Author

@jamesfredley Both cases reproduced, and are fixed in the new commit. ExplicitSortKeepsDefaultSortOffHibernateSpec has them, on a book whose mapping sorts by author.name; all five rows fail without the change.

  • A sort the call gives brings back the default sort. A map of sort properties, and a string sort on an association the query joined, were taken out of the arguments once applied, and with no sort in them the default of the mapping ran too. For list(sort: [title: 'asc']) { } that joined author again, which failed with duplicate association path: author (and with an inner join it would drop the book without an author). The sort is kept in the arguments as a sort without properties now: it adds no order, and the default stays off. The spec has the map and the string forms.
  • A sort through a path of which the query joined only the start. list(sort: ['author.publisher.name': 'asc']) { author { ... } } joined author a second time. The part of the path the query joined is reused and the rest is joined from it, so publisher is joined from the existing author join. This is for a map and for a string sort; a query that joined nothing sorts as before, and one that joined the whole path reuses it, as it did.

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 codeStyle pass.

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

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.

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.

@borinquenkid

Copy link
Copy Markdown
Member Author

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 fixes for failing calls and wrong rows (map sort, composite id sort, empty in list, the joins and the default sort) are in A below, on 7.0.x, with their note in the 7.0.18 section of the upgrade guide.
  • The one change that makes a call that runs today throw (a map sort with a direction that is not asc or desc) is in B, stacked on A, so it can wait for the list without holding the others.

The commits are the same ones, cherry-picked; the direction check is the only code that moved between the two.

A: #17052
B: #17053

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

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants