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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:14:42  来源:igfitidea点击:

Difference between findBy and findOneBy in Spring data JPA

javahibernatespring-bootspring-data-jpajpa-2.0

提问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 findBythis way? Department findByDepartmentId(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:

基本上这些是返回类型的情况:

That being said, your query definition:

话虽如此,您的查询定义:

Department findByDepartmentId(Long Id);

means that you expect a single result (because you've specified Entity Tas a return type). This will reflect on how Spring JPA executes the query - it will call getSingleResult()on the javax.persistence.Queryinterface, which will throw an exceptionif more than one objects satisfy the criteria.

意味着您期望一个结果(因为您已指定Entity T为返回类型)。这将反映在春天JPA如何执行查询-它会调用getSingleResult()上的javax.persistence.Query接口,这将抛出一个exception如果不止一个对象符合标准。

On what basis does findBydepartmentIdreturn 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 findByin place of findOneBy?

何时或为什么我不应该使用findBy代替findOneBy

Those two have different meanings and are not interchangeable.

两者含义不同,不可互换。

findOneByalways results in getSingleResult()being invoked.

findOneBy总是导致getSingleResult()被调用。

findByhas different behavior depending on the return type - as per the definitions given above.

findBy根据返回类型具有不同的行为 - 根据上面给出的定义。

回答by Mohamed Amine Mrad

findOneByXXwill ensure that there is only one or no value, if there are 2 values an exception will be thrown.

findOneByXX将确保只有一个值或没有值,如果有 2 个值将抛出异常。

However findByXXdoesn'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.

https://github.com/spring-projects/spring-data-commons/blob/14d5747f68737bb44441dc511cf16393d9d85dc8/src/main/java/org/springframework/data/repository/query/parser/PartTree.java#L65 中,它是\p{Lu}.*?部分.

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);