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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:57:14  来源:igfitidea点击:

Technical differences between Spring Data JPA's findFirst and findTop

javaspring-data-jpa

提问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'sfindFirstand findTop.

这将是非常可观的,如果有人可以扔在一些轻技术差异之间的弹簧数据JPA的findFirstfindTop

Differences, usages.

区别,用法。

Thanks

谢谢

采纳答案by SkyWalker

From Spring Data JPA - Reference Documentation,

来自Spring Data JPA - 参考文档

Limiting query results

限制查询结果

The results of query methods can be limited via the keywords firstor top, which can be used interchangeably. An optional numeric value can be appended to top/firstto 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 Distinctkeyword. Also, for the queries limiting the result set to one instance, wrapping the result into an Optional is supported.

限制表达式也支持Distinct关键字。此外,对于将结果集限制为一个实例的查询,支持将结果包装到 Optional 中。