1

I am using JOOQ for SQL building in a dynamic way, as I prefer using the good old JDBCTemplate for execution and maintaining myself the domain objects instead of generating them with JOOQ. I have the following batch insert statement: INSERT INTO my_table(field1, field2, ..., field20) values(<VALUES>);

I would like to construct this SQL in a dynamic way. For constructing a select in this manner I have used SelectQuery and methods such as addOrderBy, addConditions, join, etc.

Is there any way to add the column names (i.e. field1, field2, ..., field20) to an InsertQuery? I would like to do something like this:

InsertQuery<Record> insertQuery = ctx.insertQuery(table("my_table"));
insertQuery.addColumns("field1", "field2", ..., "field20");
for (List<Object> values : valuesList) {
    insertQuery.addValues(values);
}
getJdbcTemplate().batchUpdate(insertQuery.getSQL(), insertQuery.getValues(), batchSize);
3
  • "as I prefer using the good old JDBCTemplate for execution" - Why?
    – Lukas Eder
    Jan 13 at 15:24
  • Well, I do not want to rewrite my whole DAO layer, just want the more convenient SQL building, so I do not have to maintain that manually on each change.
    – Eszter
    Jan 13 at 16:14
  • 1
    I think you're overestimating the effort here. If you run a DML statement, both JdbcTemplate and jOOQ will provide you with an update count, except that if you go all in on jOOQ, you get much more out of it. But anyway, that wasn't the question here...
    – Lukas Eder
    Jan 13 at 16:22

1 Answer 1

1

You can use:

ctx.insertInto(table)
   .columns(columns)
   .valuesOfRows(valuesList.stream().collect(Rows.toRowList(...)))
   .execute();

Note that in jOOQ's terms, this is not a batch INSERT statement (multiple individual statements, single JDBC roundtrip), but a bulk INSERT statement (single individual statements with multiple rows). It corresponds to what you've attempted. If you prefer a Batch statement, then just create a dummy INSERT like this:

ctx.insertInto(table)
   .columns(columns)
   .values(Collections.nCopies(n, null))
   .getSQL();

And extract its SQL. Since you're not about to execute the query with jOOQ, you don't need to pass all bind values to jOOQ, only to JdbcTemplate.

Regarding the execution of jOOQ queries with JdbcTemplate

This isn't about your actual question, but I still like to mention it everywhere someone tries to execute a jOOQ query on JdbcTemplate, JDCB, JPA, etc. You'll get much more out of jOOQ if you execute the queries with jOOQ, see this blog post I've written.

1
  • I need the "multiple individual statements, single JDBC roundtrip", is there a way to get that?
    – Eszter
    Jan 13 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.