Technical differences between Spring Data JPA's findFirst and findTop

JavaSpring Data-Jpa

Java Problem Overview


I recently started working with Spring data jpa.

It would be highly appreciable if somebody could throw some light on the technical differences between Spring Data JPA's findFirst and findTop.

Differences, usages.

Thanks

Java Solutions


Solution 1 - Java

From Spring Data JPA - Reference Documentation,

Limiting query results

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. If the number is left out, a result size of 1 is assumed.

Limiting the result size of a query with Top and First

User findFirstByOrderByLastnameAsc();

User findTopByOrderByAgeDesc();

Page<User> queryFirst10ByLastname(String lastname, Pageable pageable);

Slice<User> findTop3ByLastname(String lastname, Pageable pageable);

List<User> findFirst10ByLastname(String lastname, Sort sort);

List<User> findTop10ByLastname(String lastname, Pageable pageable);

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAbdullah KhanView Question on Stackoverflow
Solution 1 - JavaSkyWalkerView Answer on Stackoverflow