Updating a primary key of an updatable record
As a good practice, a primary key should never be updated anyway. But, who am I to judge?!
By default, calling the store()
method after changing (to a non-null value) the primary key of an updatable record previously loaded via jOOQ causes an INSERT
statement to be executed. However, we can force jOOQ to generate and execute an UPDATE
of the primary key via the withUpdatablePrimaryKeys()
flag setting:
DSLContext derivedCtx = ctx.configuration().derive( new Settings().withUpdatablePrimaryKeys(true)).dsl(); SaleRecord sr = derivedCtx.selectFrom(SALE) .where(SALE.SALE_ID.eq(2L)) .fetchSingle(); sr.setSaleId(new_primary_key); sr.store(); // UPDATE primary key
Of course, we can also update the primary key via an explicit UPDATE
, and if you really have to do it, then go for this instead of a jOOQ flag:
ctx.update(SALE) .set(SALE.SALE_ID, sr.getSaleId...