Java findBy / findAllBy 之间的 Spring Data JPA 区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37253571/
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:07:13  来源:igfitidea点击:

Spring Data JPA difference between findBy / findAllBy

javaspringspring-dataspring-data-jpa

提问by Nikita

Is there any difference when using Spring Data JPA keywords between:

使用 Spring Data JPA 关键字时是否有任何区别:

List<SomeEntity> findBySomeCondition();

and

List<SomeEntity> findAllBySomeCondition();

采纳答案by Robert Hunt

No, there is no difference between them, they will execute exactly the same query, the Allpart is ignored by Spring Data when deriving the query from the method name. The only important bit is the Bykeyword, anything following it is treated as a field name (with the exception of other keywords like OrderBywhich incidentially can lead to some strange looking method names like findAllByOrderByIdAsc).

不,它们之间没有区别,它们将执行完全相同的查询,All当从方法名称派生查询时,Spring Data 会忽略该部分。唯一重要的一点是By关键字,它后面的任何内容都被视为字段名称(除了其他关键字,如OrderBy偶然会导致一些奇怪的方法名称,如findAllByOrderByIdAsc)。

This means something like this is perfectly valid:

这意味着这样的事情是完全有效的:

List<SomeEntity> findAnythingYouWantToPutHereBySomeCondition();

And will execute exactly the same SQL query as:

并且将执行与以下完全相同的 SQL 查询:

List<SomeEntity> findBySomeCondition();

or

或者

List<SomeEntity> findAllBySomeCondition();

Update:I'd never seen any official description of this behaviour in the documentation but in a recent blog postabout the upcoming 2.0 release of Spring Data (Kay) it was explained:

更新:我从未在文档中看到过对这种行为的任何官方描述,但在最近一篇关于即将发布的 Spring Data (Kay) 2.0 版本的博客文章中解释了:

Spring Data's method parsing uses prefix keywords like find, exists, count, and deleteand a terminating Bykeyword. Everything you put in between findand Bymakes your method name more expressive and does not affect query derivation.

Spring Data 的方法解析使用前缀关键字,如find, exists, count, anddelete和终止By关键字。一切你把之间find,并By让您的方法名称更具表现力,并且不影响查询的推导。

回答by shankarsh15

findBy method is used if we want to find by name or some other criteria like findByFirstName(String firstName);

如果我们想按名称或其他一些标准查找,则使用 findBy 方法 findByFirstName(String firstName);

findAll methods generally finds by providing specification

findAll 方法通常通过提供规范来查找

List<T> findAll(Specification<T> spec);

Please see docs below for more clarity:

请参阅下面的文档以获得更清晰的信息:

http://docs.spring.io/spring-data/jpa/docs/1.4.3.RELEASE/reference/html/jpa.repositories.html

http://docs.spring.io/spring-data/jpa/docs/1.4.3.RELEASE/reference/html/jpa.repositories.html

回答by Morik

one difference is that with findAllBy Hibernate filters (@Filters from org.hibernate.annotations) are applied and so a different sql.

一个区别是使用 findAllBy Hibernate 过滤器(来自 org.hibernate.annotations 的@Filters)被应用,因此使用了不同的 sql。