java 找不到匹配项时 JPA 查询的返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38480582/
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
Return value of JPA query when no matches found
提问by xenteros
I'm using Spring JPA named querys
in my repository. My problem is, that I can't find anywhere information what would be returned value for a query that wouldn't match any results. I assume it'll be null for findOne()
but I have no idea what would it be for findAllByName()
function.
我Spring JPA named querys
在我的存储库中使用。我的问题是,我无法在任何地方找到与任何结果都不匹配的查询的返回值的信息。我假设它为空,findOne()
但我不知道它的findAllByName()
功能是什么。
Does anyone know from his/her experience or know a place in documentation?
有没有人从他/她的经验中知道或知道文档中的某个位置?
回答by exoddus
From my little and personal experience, if you search for an object on your repo, for example by Id or Name the named query method returns an object of type T
, but if no results are found from your repo, it will return null.
根据我的个人经验,如果你在你的仓库中搜索一个对象,例如通过 Id 或 Name 命名查询方法返回一个 type 的对象T
,但如果你的仓库没有找到结果,它将返回 null。
Methods that can return more than one element, will produce an empty collection List<T>
(not null).
可以返回多个元素的方法将产生一个空集合List<T>
(非空)。
Some documentation here: http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repository-query-keywords
这里的一些文档:http: //docs.spring.io/spring-data/jpa/docs/current/reference/html/#repository-query-keywords
Appendix D: Repository query return types
Supported query return types Query return types:
T An unique entity. Expects the query method to return one result at most. In case no result is found null is returned. More than one result will trigger an IncorrectResultSizeDataAccessException.
Iterator An Iterator.
附录 D:存储库查询返回类型
支持的查询返回类型查询返回类型:
T 一个独特的实体。期望查询方法最多返回一个结果。如果未找到结果,则返回 null。多个结果将触发 IncorrectResultSizeDataAccessException。
迭代器 一个迭代器。
Seems like only when return type is of type T
is the only one that specify a null is returned if no matches.
似乎只有当返回类型为 type 时T
,如果没有匹配项,则返回唯一指定为 null的类型。