HHH-16931 document getResultCount()
This commit is contained in:
parent
a562ab2462
commit
9bb15727e4
|
@ -838,7 +838,7 @@ So if there are any unflushed changes to ``Book``s, this query might return stal
|
|||
[source,java]
|
||||
----
|
||||
List<Book> 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<Book> 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<Book> 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<Book> 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<Book> 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<Book> 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<Book> query =
|
||||
session.createSelectionQuery("from Book where title like ?1 order by title", Book.class)
|
||||
.setParameter(1, titlePattern);
|
||||
long pages = query.getResultCount() / MAX_RESULTS;
|
||||
List<Book> 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<Book> 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]]
|
||||
|
|
Loading…
Reference in New Issue