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 the database-generated primary key

A typical scenario consists of fetching a database-generated (identity) primary key after an INSERT operation is executed via the insertInto()method or the updatable record's insert()method. If you are using insertInto() (DSL.insertInto() or DSLContext.insertInto()), the database-generated primary key can be obtained via the returningResult()/returning() methods. For instance, the identity primary key of SALE is shaped in MySQL via AUTO_INCREMENT, in SQL Server via IDENTITY, and for historic reasons (because both now support standard SQL IDENTITY columns), in PostgreSQL and Oracle via database sequences. In all these cases, the generated identity primary key of SALE can be fetched as here (SALE.SALE_ID):

long insertedId = ctx.insertInto(SALE, SALE.FISCAL_YEAR, 
    SALE.SALE_, SALE.EMPLOYEE_NUMBER, SALE.FISCAL_MONTH, 
    SALE.REVENUE_GROWTH)
 .values(2004, 2311.42, 1370L, 1, 0.0)
 .returningResult...
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