Home > database >  How to specify "onDuplicateXYZ" with a record?
How to specify "onDuplicateXYZ" with a record?

Time:10-11

I want to insert a batch of records into a MySQL table, but ignore those who are already there (duplicate primary key).

You can specify an "onDuplicate" strategy when using "dslContext.insert":

create.insertInto(AUTHOR, AUTHOR.ID, AUTHOR.LAST_NAME)
      .values(3, "Koontz")
      .onDuplicateKeyIgnore()
      .set(AUTHOR.LAST_NAME, "Koontz")
      .execute();

The question is, how can I do the same with records? Something like this:

var record = dslContext.newRecord(AUTHOR);
record.setField("123");
record.onDuplicateKeyIgnore();
record.store()

CodePudding user response:

There's no such convenience method in jOOQ. The tradeoff here is to assess whether any given convenience (e.g. INSERT .. IGNORE) is worth the extra API surface and the dozens of feature interactions this brings. You could try your luck with a feature request. It won't be implemented soon, but at least, that would allow for collecting upvotes to see community interest in such a feature (e.g. UpdatableRecord::merge was added due to popular demand).

A simple workaround is to try the record.insert() call, and catch and ignore constraint violation exceptions.

  • Related