Reputation:
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
Reputation: 220762
Yes, you can do that!
You can batch-delete records through
DSLContext.batchDelete(UpdatableRecord...)
orDSLContext.batchDelete(Collection<? extends UpdatableRecord<?>>)
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.
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