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?
When there are no rows, both query. list() and criteria. list() are returning empty list instead of a null value.
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.
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.
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[].
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With