
jOOQ Masterclass
By :

jOOQ has come with three handy methods named fetchOne()
, fetchSingle()
, and fetchAny()
. All three are capable of returning a resulting record, but each of them will do this under certain coordinates. So, let's go through each method in detail.
For instance, the fetchOne()
method returns, at most, one resulting record. In other words, if the fetched result set has more than one record, then fetchOne()
throws a jOOQ-specific TooManyRowsException
exception. But if the result set has no records, then fetchOne()
returns null
. In this context, fetchOne()
can be useful for fetching a record by a primary key, other unique keys, or a predicate that guarantees uniqueness, while you prepare to handle potentially null
results. Here is an example of using fetchOne()
:
EmployeeRecord result = ctx.selectFrom(EMPLOYEE) .where(EMPLOYEE.EMPLOYEE_NUMBER.eq(1370L)) .fetchOne();
Alternatively...