问题描述
在使用Spring Data JPA关键字时有什么区别:
Is there any difference when using Spring Data JPA keywords between:
List<SomeEntity> findBySomeCondition();
和
List<SomeEntity> findAllBySomeCondition();
推荐答案
不,它们之间没有区别,它们将执行完全相同的查询,All
部分被Spring Data忽略时从方法名称派生查询.唯一重要的一点是 By
关键字,它后面的任何内容都被视为字段名称(除了 OrderBy
等其他关键字可能会导致一些奇怪的方法findAllByOrderByIdAsc
之类的名称).
No, there is no difference between them, they will execute exactly the same query, the All
part is ignored by Spring Data when deriving the query from the method name. The only important bit is the By
keyword, anything following it is treated as a field name (with the exception of other keywords like OrderBy
which incidentially can lead to some strange looking method names like findAllByOrderByIdAsc
).
这意味着这样的事情是完全有效的:
This means something like this is perfectly valid:
List<SomeEntity> findAnythingYouWantToPutHereBySomeCondition();
并且将执行完全相同的 SQL 查询:
And will execute exactly the same SQL query as:
List<SomeEntity> findBySomeCondition();
或
List<SomeEntity> findAllBySomeCondition();
文档 讨论了这个特性:
find
(或其他引入关键字)和 By
之间的任何文本都被认为是描述性的,除非使用限制结果的关键字之一,例如 Distinct
在要创建的查询上设置不同的标志或 Top
/First
来限制查询结果.
Any text between
find
(or other introducing keywords) andBy
is considered to be descriptive unless using one of the result-limiting keywords such as aDistinct
to set a distinct flag on the query to be created orTop
/First
to limit query results.
功能的目的在 博客文章 关于即将发布的 Spring Data 2.0 版本:
The purpose of feature was explained in a blog post about the then-upcoming 2.0 release of Spring Data:
Spring Data 的方法解析使用前缀关键字,如 find
、exists
、count
和 delete
以及终止按
关键字.您在 find
和 By
之间添加的所有内容都使您的方法名称更具表现力,并且不会影响查询派生.
Spring Data’s method parsing uses prefix keywords like
find
,exists
,count
, anddelete
and a terminatingBy
keyword. Everything you put in betweenfind
andBy
makes your method name more expressive and does not affect query derivation.
这篇关于Spring Data JPA findBy/findAllBy 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!