Spring Data JPA与findBy / findAllBy的区别 [英] Spring Data JPA difference between findBy / findAllBy
问题描述
使用Spring Data JPA关键字时有什么不同:
Is there any difference when using Spring Data JPA keywords between:
List<SomeEntity> findBySomeCondition();
和
List<SomeEntity> findAllBySomeCondition();
推荐答案
不,他们之间没有区别,他们会执行完全相同的查询时,Spring Data会在从方法名称派生查询时忽略 All
部分。唯一重要的一点是按
关键字,其后面的任何内容都被视为字段名称(除了 OrderBy $ c等其他关键字) $ c>这可能导致一些奇怪的方法名称,如
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();
更新:我从未见过有关此行为的任何官方说明文档,但在最近的博客文章关于即将发布的Spring Data(Kay)2.0版本的解释:
Update: I'd never seen any official description of this behaviour in the documentation but in a recent blog post about the upcoming 2.0 release of Spring Data (Kay) it was explained:
Spring Data的方法解析使用前缀关键字,如
find
,exists
,count
,删除
和终止按
关键字。你在找到
和之间放置的所有东西都会使你的方法名更具表现力,不会影响查询推导。
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的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!