Repository navigation
Upgrade notes
Need to do
- Use Spring Boot 4.1 or later, as for all releases since v2.6.0. On Spring Boot 4.0 (Hibernate 7.1 and 7.2),
QueryTransformer.asPagecan fail at runtime with aNoSuchMethodError: the copy of a list parameter with an explicitTemporalTypecallsQueryParameterBinding.setBindValues(Collection, TemporalType), which exists only since Hibernate 7.4. - If you page a criteria query with
QueryTransformer.asPageoutside a transaction, create the query in the transaction that pages it, or use an HQL query. See "Need to know".
Need to know
QueryTransformer.asPage now reads the content first. Then it counts on the given query only when the session of the query is still open. This is the case for a query that was created in the current transaction, with Open EntityManager in View, or with an application-managed EntityManager. In all other cases, Spring closes the session after the content call, and the count runs on a second query that is created from the query string.
These changes can make a call fail that passed with 2.7.0, when the session of the query is closed and a count is necessary:
- A criteria query throws an
IllegalStateException, because it cannot be created a second time from a query string. - An HQL query without a
fromclause, which gets its entity from the result type, fails with aSemanticExceptionof Hibernate. This is not supported.
These changes can make a call pass that failed with 2.7.0:
asPageworks without a transaction on Spring Boot 4.1.3. Since Spring Framework 7.0.11, Spring closes theEntityManagerof a query aftergetResultCount(), so the content call failed with "Session/EntityManager is closed".asPageworks for a query that is created before the transaction that pages it. The count then sees the changes of the transaction that are not committed.
Queries that the count does not support
HQL and criteria queries. These limits are new in 2.8.0. They apply only when the session of the query is closed after the content call and a count is necessary:
// A criteria query: IllegalStateException
entityManager.createQuery(criteriaQuery);
// An HQL query without a "from" clause: SemanticException, "query has no 'select' clause, and no root entities"
entityManager.createQuery("where name <> :name", Person.class);Native queries. These limits are not new, 2.7.0 has them too. For a native query, the count is select count(*) from (<query>) as count, with each line break of the query replaced by a space. Tested on PostgreSQL:
-- A line comment: the rest of the count query becomes a comment. Syntax error.
select name from person order by name -- by name
-- A named parameter: NullPointerException. Use a positional parameter (?1).
select name from person where name <> :name
-- A semicolon at the end: syntax error.
select name from person order by name;
-- A line break in a string literal: no error, but the count looks for 'line 1 line 2', so the total is wrong.
select name from person where note = 'line 1
line 2'A query in a text block, a block comment (/* ... */), a with clause and two columns with the same name work.
Good to know
- The count statement does not run when the content already shows the total, for example on a first page that is not full. A test that counts the SQL statements can see one statement less.
- The second query gets each named or positional parameter with its bind type and its temporal precision.
- Without an
EntityManagerbound to the thread, the second query runs on its ownEntityManager, which is closed after the count. The content and the count use their connections in sequence, not at the same time. - Native queries are not changed.
- This library is now built with Spring Boot 4.1.3.
What's Changed
- Add the review freshness workflow by @SirCotare in #79
- Fix the paging without a transaction on Spring Boot 4.1.3 by @ThoSap in #78
- Count the page on a copy of the query without the SQM statement by @Piiit in #80
Full Changelog: v2.7.0...v2.8.0