Search code examples
javasqljooq

jOOQ - difference between fetchAny and fetchOne


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.


Solution

  • The intent of the two methods is different:

    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.