Java Spring数据JPA中findBy和findOneBy的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45988295/
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
Difference between findBy and findOneBy in Spring data JPA
提问by Arun Gowda
All i know so far is that FindBy can return multiple results while FindOneBy will return a single result or null when we use it the following way.
到目前为止我所知道的是 FindBy 可以返回多个结果,而 FindOneBy 将返回单个结果或 null 当我们使用以下方式时。
List<Department> findByDepartmentName(String name);
Department findOneByDepartmentId(Long Id);
now, my question is, can i use findBy this way?
现在,我的问题是,我可以这样使用 findBy 吗?
Department findByDepartmentId(Long Id);
If yes,
如果是,
- Lets assume there are multiple records for given Id.
- On what basis does findBydepartmentIdreturn a single record?
- 让我们假设给定的 Id 有多个记录。
- findBydepartmentId在什么基础上 返回单个记录?
Finally, When or Why should i not use findBy in place of findOneBy?
最后,何时或为什么不应该使用 findBy 代替 findOneBy?
采纳答案by hovanessyan
Can I use
findBy
this way? DepartmentfindByDepartmentId(Long Id)
;
我可以用
findBy
这种方式吗?部门findByDepartmentId(Long Id)
;
Yes, this syntax is technically correct from Spring JPA point of view. Although Spring JPA infers what you're trying to achieve with your query looking at the return typeas well.
是的,从 Spring JPA 的角度来看,这种语法在技术上是正确的。尽管 Spring JPA 也会通过查看返回类型的查询来推断您要实现的目标。
Basically these are the cases for return types:
基本上这些是返回类型的情况:
with your query you want to return a single value - you can specify
basic type
,Entity T
,Optional<T>
,CompletableFuture<T>
etc.with your query you want to return a collection of T - you can specify
List<T>
,Stream<T>
,Page<T>
,Slice<T>
etc.
与查询要返回一个值-您可以指定
basic type
,Entity T
,Optional<T>
,CompletableFuture<T>
等。
That being said, your query definition:
话虽如此,您的查询定义:
Department findByDepartmentId(Long Id);
means that you expect a single result (because you've specified Entity T
as a return type). This will reflect on how Spring JPA executes the query - it will call getSingleResult()
on the javax.persistence.Query
interface, which will throw an exception
if more than one objects satisfy the criteria.
意味着您期望一个结果(因为您已指定Entity T
为返回类型)。这将反映在春天JPA如何执行查询-它会调用getSingleResult()
上的javax.persistence.Query
接口,这将抛出一个exception
如果不止一个对象符合标准。
On what basis does
findBydepartmentId
return a single record?
在什么基础上
findBydepartmentId
返回单个记录?
On the basis that there's a single object with that Id, otherwise it will throw an exception.
基于有一个具有该 Id 的对象,否则它将抛出异常。
When or Why should i not use
findBy
in place offindOneBy
?
何时或为什么我不应该使用
findBy
代替findOneBy
?
Those two have different meanings and are not interchangeable.
两者含义不同,不可互换。
findOneBy
always results in getSingleResult()
being invoked.
findOneBy
总是导致getSingleResult()
被调用。
findBy
has different behavior depending on the return type - as per the definitions given above.
findBy
根据返回类型具有不同的行为 - 根据上面给出的定义。
回答by Mohamed Amine Mrad
findOneByXX
will ensure that there is only one or no value, if there are 2 values an exception will be thrown.
findOneByXX
将确保只有一个值或没有值,如果有 2 个值将抛出异常。
However findByXX
doesn't make this check of uniqueness.
但是findByXX
,不会进行这种唯一性检查。
回答by Norabal
I have done some tests and Spring Data ignore all characters between the method (find
, delete
,...) and By
.
我做了一些测试,Spring Data 忽略了方法 ( find
, delete
,...) 和By
.
In https://github.com/spring-projects/spring-data-commons/blob/14d5747f68737bb44441dc511cf16393d9d85dc8/src/main/java/org/springframework/data/repository/query/parser/PartTree.java#L65it is the \p{Lu}.*?
part.
Spring Data only use return type to decide how handle responses.
Spring Data 仅使用返回类型来决定如何处理响应。
So it is possible to define these following methods even if it is not correct semantically.
因此,即使在语义上不正确,也可以定义以下这些方法。
Department findAllByDepartmentId(Long Id);
List<Department> findOneByDepartmentName(String name);