Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate query.list() method is returning empty list instead of null value

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?

like image 447
Reddy Avatar asked Aug 30 '10 09:08

Reddy


People also ask

Does query for list return null?

When there are no rows, both query. list() and criteria. list() are returning empty list instead of a null value.

Is Empty list same as null?

An empty collection isn't the same as null . An empty collection is actually a collection, but there aren't any elements in it yet. null means no collection exists at all.

How hibernate handle null values?

The best way to avoid Hibernate's attempts at setting null values to primitives is to use Wrapper classes (Integer, Long, Double...); and especially, if you need to tack on a column or 2 to an existing table.

What is query list Return hibernate?

list. Return the query results as a List. If the query contains multiple results pre row, the results are returned in an instance of Object[].


1 Answers

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

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.

like image 60
Péter Török Avatar answered Sep 17 '22 15:09

Péter Török