The hibernate query.list () method returns an empty list instead of a null value

If there are no rows, then both query.list () and criteria.list () return an empty list instead of a null value. What is the reason for this?

+44
java collections nullpointerexception hibernate return-value
Aug 30 '10 at 9:48
source share
2 answers

The reason is not to force null checks into client code, according to Effective Java 2nd Edition , Paragraph 43: Returning arrays or collections empty, not zeros .

This makes client code simpler and less error prone (and, most likely, also a method implementation).

The zero-return idiom is probably a delay from the C programming language; the length of the arrays is returned separately from the real arrays. In C there is no advantage in allocating an array if zero is returned as length.

+85
Aug 30 '10 at 9:52
source share

It is consistent: the list is returned with all the results, whether they are or not.

+10
Aug 30 '10 at 9:50
source share



All Articles