Is there a (real) difference between fetchAny()
and fetchOne()
? Both return exact one record. The API documentation is the same, but the implementation (on github) is different.
The intent of the two methods is different:
Returns:
The resulting record or null if the query returns no records.
Throws:
TooManyRowsException
- if the query returned more than one record
Returns:
The first resulting record or null if the query returns no records.
In essence, when you use fetchOne()
the query must return 0 or 1 record. When you use fetchAny()
the query may return any number of records, and if any record is returned by the database, the first one fetched from the JDBC result set will be returned.
Notice that fetchOne()
will thus try to fetch 2 records from the JDBC driver (to decide whether TooManyRowsException
needs to be thrown), while fetchAny()
only fetches at most 1 record.