user2814395
user2814395

Reputation:

JOOQ:Delete List of record in one shot?

How to delete List of records from a single query in JOOQ? Is this possible with JOOQ API? Or i have to delete record one by one ,Just get one record fire query and so on?

Upvotes: 7

Views: 7057

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 220762

Yes, you can do that!

Using a batch statement

You can batch-delete records through

Example:

MyTableRecord record1 = //...
MyTableRecord record2 = //...
DSL.using(configuration).batchDelete(record1, record2).execute();

This will generate a JDBC batch statement, which can be executed much faster than single deletes.

Using a single DELETE statement with an IN predicate

Another option would be to create a single DELETE statement as such:

DSLContext create = DSL.using(configuration);
// This intermediate result is only used to extract ID values later on
Result<MyTableRecord> result = create.newResult(MY_TABLE);
result.add(record1);
result.add(record2);

create.delete(MY_TABLE)
      .where(MY_TABLE.ID.in(result.getValues(MY_TABLE.ID))
      .execute();

Upvotes: 5

Related Questions