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