Spring Data JPA与findBy / findAllBy的区别 [英] Spring Data JPA difference between findBy / findAllBy

查看:14473
本文介绍了Spring Data JPA与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 这可能导致一些奇怪的方法名称,如 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, and delete and a terminating By keyword. Everything you put in between find and By makes your method name more expressive and does not affect query derivation.

这篇关于Spring Data JPA与findBy / findAllBy的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆