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

查看:96
本文介绍了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();

推荐答案

不,它们之间没有区别,它们将执行完全相同的查询,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) and By is considered to be descriptive unless using one of the result-limiting keywords such as a Distinct to set a distinct flag on the query to be created or Top/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 的方法解析使用前缀关键字,如 findexistscountdelete 以及终止 关键字.您在 findBy 之间添加的所有内容都使您的方法名称更具表现力,并且不会影响查询派生.

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天全站免登陆