8

What I actually want is to write following query in JOOQ:

stmt = connection.prepareStatement(
    "INSERT INTO `tbl` (`name`, `service_id`, `device_id`) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE `id` = LAST_INSERT_ID(`id`)", 
    Statement.RETURN_GENERATED_KEYS
);

I'm not able to find a way to do this in JOOQ. Is it possible?

Majid Azimi
  • 5,575
  • 13
  • 64
  • 113

2 Answers2

7

Currently (as of jOOQ 3.4-3.6), that's not possible due to a flaw in jOOQ's INSERT API:

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
1

This might help you. For time being I am using this:

Field<Integer> LAST_INSERT_ID = DSL.function("LAST_INSERT_ID", Integer.class, PACKAGE.ID);

dsl.insertInto(PACKAGE)
   .set(dsl.newRecord(PACKAGE, packagePojo))
   .onDuplicateKeyUpdate()
   .set(PACKAGE.ID, LAST_INSERT_ID)
   .set(PACKAGE.PTR_JOB, packagePojo.getPtrJob())
   .set(PACKAGE.PACK_NUMBER, packagePojo.getPackNumber())
   .set(PACKAGE.RACK, packagePojo.getRack()
Tomas
  • 33
  • 7