Hibernate query.list() 方法返回空列表而不是空值 [英] Hibernate query.list() method is returning empty list instead of null value
问题描述
当没有行时,query.list()
和 criteria.list()
都返回 空 列表而不是 <代码>空代码>值.
When there are no rows, both query.list()
and criteria.list()
are returning empty list instead of a null
value.
这背后的原因是什么?
推荐答案
原因是不强制在客户端代码中检查空值,与 Effective Java 2nd Edition,Item 43:返回空数组或集合,而不是空值.
The reason is not to force null checks in client code, in consistency with Effective Java 2nd Edition, Item 43: Return empty arrays or collections, not nulls.
这使得客户端代码更简单,更不容易出错(很可能也是方法实现).
This makes the client code simpler and less error-prone (and most likely the method implementation as well).
null-return 惯用语可能是 C 编程语言的遗留物,在哪些数组长度与实际数组分开返回.在 C 中,没有如果返回零作为长度,则分配数组的优势.
The null-return idiom is likely a holdover from the C programming language, in which array lengths are returned separately from actual arrays. In C, there is no advantage to allocating an array if zero is returned as the length.
这篇关于Hibernate query.list() 方法返回空列表而不是空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!