Java Spring Data JPA 的 findFirst 和 findTop 之间的技术差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38045439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Technical differences between Spring Data JPA's findFirst and findTop
提问by Abdullah Khan
I recently started working with Spring data jpa.
我最近开始使用 Spring 数据 jpa。
It would be highly appreciable if somebody could throw some light on the technical differencesbetween Spring Data JPA'sfindFirst
and findTop
.
这将是非常可观的,如果有人可以扔在一些轻技术差异之间的弹簧数据JPA的findFirst
和findTop
。
Differences, usages.
区别,用法。
Thanks
谢谢
采纳答案by SkyWalker
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.
查询方法的结果可以通过关键字first
或进行限制top
,可以互换使用。可以附加一个可选的数值top/first
来指定要返回的最大结果大小。如果忽略该数字,则假定结果大小为 1。
Limiting the result size of a query with Top and First
使用 Top 和 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.
限制表达式也支持Distinct
关键字。此外,对于将结果集限制为一个实例的查询,支持将结果包装到 Optional 中。