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. What is the reason behind this?
推荐答案
原因是不强制在客户端代码中的空检查,与 Effective Java第2版,第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返回成语可能是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屋!