Skip to content

Commit 3f54bcd

Browse files
author
Thomas Darimont
committed
DATACMNS-580 - Add section on limiting the query results to reference documentation.
Added section explaining the use of first / top keywords to the reference doc. Updated the keyword list. Original pull request: #99.
1 parent 40ba027 commit 3f54bcd

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/main/asciidoc/repositories.adoc

+31-1
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,42 @@ List<User> findByLastname(String lastname, Pageable pageable);
296296
----
297297
====
298298

299-
The first method allows you to pass an `org.springframework.data.domain.Pageable` instance to the query method to dynamically add paging to your statically defined query. A `Page` knows about the total number of elements and pages available. It does so by the infrastructure triggering a count query to calculate the overall number. As this might be expensive depending on the store used, `Slice` can be used as return instead. A `Slice` only knows about whether there's a next `Slice` available which might be just sufficient when walking thought a larger resut set.
299+
The first method allows you to pass an `org.springframework.data.domain.Pageable` instance to the query method to dynamically add paging to your statically defined query. A `Page` knows about the total number of elements and pages available. It does so by the infrastructure triggering a count query to calculate the overall number. As this might be expensive depending on the store used, `Slice` can be used as return instead. A `Slice` only knows about whether there's a next `Slice` available which might be just sufficient when walking thought a larger result set.
300300

301301
Sorting options are handled through the `Pageable` instance too. If you only need sorting, simply add an `org.springframework.data.domain.Sort` parameter to your method. As you also can see, simply returning a `List` is possible as well. In this case the additional metadata required to build the actual `Page` instance will not be created (which in turn means that the additional count query that would have been necessary not being issued) but rather simply restricts the query to look up only the given range of entities.
302302

303303
NOTE: To find out how many pages you get for a query entirely you have to trigger an additional count query. By default this query will be derived from the query you actually trigger.
304304

305+
[[repositories.limit-query-result]]
306+
=== Limiting query results
307+
308+
The results of query methods can be limited via the keywords `first` or `top`, which can be used interchangeably. An optional numeric value can be appended to top/first to specify the maximum result size to be returned.
309+
If the number is left out, a result size of 1 is assumed.
310+
311+
.Limiting the result size of a query with `Top` and `First`
312+
====
313+
[source, java]
314+
----
315+
User findFirstByOrderByLastname();
316+
317+
User findTopByOrderByAgeDesc();
318+
319+
Page<User> queryFirst10ByLastname(String lastname, Pageable pageable);
320+
321+
Slice<User> findTop3ByLastname(String lastname, Pageable pageable);
322+
323+
List<User> findFirst10ByLastname(String lastname, Sort sort);
324+
325+
List<User> findTop10ByLastname(String lastname, Pageable pageable);
326+
----
327+
====
328+
329+
The limiting expressions also support the `Distinct` keyword. Also, for the queries limiting the result set to one instance, wrapping the result into an `Optional` is supported.
330+
331+
If pagination or slicing is applied to a limiting query pagination (and the calculation of the number of pages available) then it is applied within the limited result.
332+
333+
NOTE: Note that limiting the results in combination with dynamic sorting via a `Sort` parameter allows to express query methods for the 'K' smallest as well as for the 'K' biggest elements.
334+
305335
[[repositories.create-instances]]
306336
== Creating repository instances
307337
In this section you create instances and bean definitions for the repository interfaces defined. One way to do so is using the Spring namespace that is shipped with each Spring Data module that supports the repository mechanism although we generally recommend to use the Java-Config style configuration.

src/main/asciidoc/repository-query-keywords-reference.adoc

+2
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,7 @@ The following table lists the keywords generally supported by the Spring Data re
3535
|`STARTING_WITH`|`StartingWith`, `IsStartingWith`, `StartsWith`
3636
|`TRUE`|`True`, `IsTrue`
3737
|`WITHIN`|`Within`, `IsWithin`
38+
|`TOP`|`Top`
39+
|`FIRST`|`First`
3840
|===============
3941

0 commit comments

Comments
 (0)