Reader small image

You're reading from  jOOQ Masterclass

Product typeBook
Published inAug 2022
Reading LevelBeginner
PublisherPackt
ISBN-139781800566897
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Anghel Leonard
Anghel Leonard
author image
Anghel Leonard

Anghel Leonard is a Chief Technology Strategist and independent consultant with 20+ years of experience in the Java ecosystem. In daily work, he is focused on architecting and developing Java distributed applications that empower robust architectures, clean code, and high-performance. Also passionate about coaching, mentoring and technical leadership. He is the author of several books, videos and dozens of articles related to Java technologies.
Read more about Anghel Leonard

Right arrow

Fetching one record, a single record, or any record

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.

Using fetchOne()

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

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
jOOQ Masterclass
Published in: Aug 2022Publisher: PacktISBN-13: 9781800566897

Author (1)

author image
Anghel Leonard

Anghel Leonard is a Chief Technology Strategist and independent consultant with 20+ years of experience in the Java ecosystem. In daily work, he is focused on architecting and developing Java distributed applications that empower robust architectures, clean code, and high-performance. Also passionate about coaching, mentoring and technical leadership. He is the author of several books, videos and dozens of articles related to Java technologies.
Read more about Anghel Leonard