From 9bb15727e45ed9f2fd572fe1c02655de623fb0f8 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Mon, 19 Feb 2024 22:55:22 +0100 Subject: [PATCH] HHH-16931 document getResultCount() --- .../asciidoc/introduction/Interacting.adoc | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/documentation/src/main/asciidoc/introduction/Interacting.adoc b/documentation/src/main/asciidoc/introduction/Interacting.adoc index 220219786a..095114abc8 100644 --- a/documentation/src/main/asciidoc/introduction/Interacting.adoc +++ b/documentation/src/main/asciidoc/introduction/Interacting.adoc @@ -838,7 +838,7 @@ So if there are any unflushed changes to ``Book``s, this query might return stal [source,java] ---- List books = - session.createNativeQuery("select * from Books") + session.createNativeQuery("select * from Books", Book.class) .getResultList() ---- @@ -849,7 +849,7 @@ Either, we could simply force a flush by calling `flush()` or by setting the flu [source,java] ---- List books = - session.createNativeQuery("select * from Books") + session.createNativeQuery("select * from Books", Book.class) .setHibernateFlushMode(ALWAYS) .getResultList() ---- @@ -859,7 +859,7 @@ Or, alternatively, we could tell Hibernate which modified state affects the resu [source,java] ---- List books = - session.createNativeQuery("select * from Books") + session.createNativeQuery("select * from Books", Book.class) .addSynchronizedEntityClass(Book.class) .getResultList() ---- @@ -893,7 +893,7 @@ For example, this: [source,java] ---- List books = - session.createSelectionQuery("from Book where title like ?1 order by title") + session.createSelectionQuery("from Book where title like ?1 order by title", Book.class) .setParameter(1, titlePattern) .setMaxResults(MAX_RESULTS) .getResultList(); @@ -903,8 +903,9 @@ is simpler than: [source,java] ---- +// a worse way to do pagination List books = - session.createSelectionQuery("from Book where title like ?1 order by title fetch first ?2 rows only") + session.createSelectionQuery("from Book where title like ?1 order by title fetch first ?2 rows only", Book.class) .setParameter(1, titlePattern) .setParameter(2, MAX_RESULTS) .getResultList(); @@ -915,12 +916,23 @@ Hibernate's `SelectionQuery` has a slightly different way to paginate the query [source,java] ---- List books = - session.createSelectionQuery("from Book where title like ?1 order by title") + session.createSelectionQuery("from Book where title like ?1 order by title", Book.class) .setParameter(1, titlePattern) .setPage(Page.first(MAX_RESULTS)) .getResultList(); ---- +The `getResultCount()` method is useful for displaying the number of pages of results: + +[source,java] +---- +SelectionQuery query = + session.createSelectionQuery("from Book where title like ?1 order by title", Book.class) + .setParameter(1, titlePattern); +long pages = query.getResultCount() / MAX_RESULTS; +List books = query.setMaxResults(MAX_RESULTS).getResultList(); +---- + A closely-related issue is ordering. It's quite common for pagination to be combined with the need to order query results by a field that's determined at runtime. So, as an alternative to the HQL `order by` clause, `SelectionQuery` offers the ability to specify that the query results should be ordered by one or more fields of the entity type returned by the query: @@ -928,7 +940,7 @@ So, as an alternative to the HQL `order by` clause, `SelectionQuery` offers the [source,java] ---- List books = - session.createSelectionQuery("from Book where title like ?1") + session.createSelectionQuery("from Book where title like ?1", Book.class) .setParameter(1, titlePattern) .setOrder(List.of(Order.asc(Book._title), Order.asc(Book._isbn))) .setMaxResults(MAX_RESULTS) @@ -946,6 +958,7 @@ Unfortunately, there's no way to do this using JPA's `TypedQuery` interface. | `setFirstResult()` | Set an offset on the results returned by a query | ✔ | `setPage()` | Set the limit and offset by specifying a `Page` object | ✖ | `setOrder()` | Specify how the query results should be ordered | ✖ +| `getResultCount()` | Determine how many results the query would return in the absence of any limit or offset | ✖ |=== [[projection-lists]]