0

Is there any possibility to specify the sequence generator as a value for a column in SimpleJdbcInsert, JdbcTemplate, NamedParameterJdbcTemplate or any other class for batch execution?

e.g., I want to achieve the SQL to be generated by any of the above classes as:

INSERT INTO SOME_TABLE_NAME (ID, COLUMN_A,...) VALUES (SOME_SEQUENCE.NEXTVAL, 'value for column A', ...);

A sample code snippet is like:

import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;

List<SomeTableEntity> listOfEntities; // received from method parameter
SimpleJdbcInsert sql = new SimpleJdbcInsert(dataSource).withTableName("SOME_TABLE_NAME");
SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(listOfEntities.toArray());
sql.executeBatch(batch);

I tried to trick the SimpleJdbcInsert as:

SqlParameterSource id = new MapSqlParameterSource("ID", "SOME_SEQUENCE.nextval"));
SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(listOfEntities.toArray());
List<SqlParameterSource> params = new ArrayList<>(batch.length + 1);
params.add(id);
for (SqlParameterSource param : batch)
{
    params.add(param);
}
sql.executeBatch(params.toArray(new SqlParameterSource[] {}));

But not to a surprise, that didn't worked since the ID column is of type numeric and it tried to fill the value as "SOME_SEQUENCE.nextval" instead of evaluating the result of SOME_SEQUENCE.nextval.


PS: There are too many columns in the table and due to that I do not want to use a prepared statement solution

String sql = "INSERT INTO USER 
        (USER_PK, ACCOUNTNUMBER, FIRSTNAME, LASTNAME, EMAIL ) 
       VALUES 
        (user.nextval, ?, ?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, accountNumber);
ps.setString(2, firstName);
ps.setString(3, lastName);
ps.setString(4, email);
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
  • Do you mean you want the sequence number of another table? – Mehrdad HosseinNejad Yami Feb 05 '20 at 09:09
  • @MehrdadHosseinNejad: No. I have/will create a sequence which will be used to fill the value for the `ID` column. – Mohammad Faisal Feb 05 '20 at 09:35
  • I don't think it's possible. Currently we run a query before the main `insert` to retrieve the next id in the relevant sequence and then add that value into the query. – mohammedkhan Feb 18 '20 at 10:39
  • @mohammedkhan: But in that case you would not be able to take advantage of [SimpleJdbcInsert.executeBatch()](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/core/simple/SimpleJdbcInsert.html#executeBatch-org.springframework.jdbc.core.namedparam.SqlParameterSource...-). – Mohammad Faisal Feb 18 '20 at 11:09
  • @MohammadFaisal Well if the details you're inserting into the db are derived from POJOs you could set the ids from the sequence onto your objects and then run a batch update. If that isn't the case then no, it's not possible to run it in a batch. You'll have to run each `INSERT` statement separately. – mohammedkhan Feb 20 '20 at 09:59
  • @mohammedkhan agree to your comment. – Mohammad Faisal Feb 20 '20 at 10:08

0 Answers0