UPSERT in PostgreSQL using jOOQ UPSERT in PostgreSQL using jOOQ postgresql postgresql

UPSERT in PostgreSQL using jOOQ


jOOQ 3.7+ supports PostgreSQL 9.5's ON CONFLICT clause:

The full PostgreSQL vendor-specific syntax is not yet supported, but you can use the MySQL or H2 syntax, which can both be emulated using PostgreSQL's ON CONFLICT:

MySQL INSERT .. ON DUPLICATE KEY UPDATE:

DSL.using(configuration)   .insertInto(TABLE)   .columns(ID, A, B)   .values(1, "a", "b")   .onDuplicateKeyUpdate()   .set(A, "a")   .set(B, "b")   .execute();

H2 MERGE INTO ..

DSL.using(configuration)   .mergeInto(TABLE, A, B, C)   .values(1, "a", "b")   .execute();


Here is an upsert utility method derived from Lucas' solution above for UpdatableRecord objects:

public static int upsert(final DSLContext dslContext, final UpdatableRecord record) {    return dslContext.insertInto(record.getTable())                     .set(record)                     .onDuplicateKeyUpdate()                     .set(record)                     .execute();}


Seems a bit of a complicated way of achieving the objective. Why not use a simple stored fucntion? how to create an upsert function is described in the postgresql manual then just call it from your java code.