Вот ответ.
1) Одним из решений, если вы используете jdbcTemplate
(Spring JDB C), является предварительное резервирование собственного диапазона идентификаторов. Затем укажите вручную рассчитанные идентификаторы для каждой строки. Например,
@Transactional(readOnly = false, rollbackFor = Exception.class)
public void doMultiTableInsert(List<String> entries) throws Exception {
// 1. Obtain current Sequence values
Integer currTable1SeqVal = table1DAO.getCurrentTable1SeqVal();
Integer currTable2SeqVal = table2DAO.getCurrentTable2SeqVal();
// 2. Immediately update the Sequences to the calculated final value (this reserves the ID range immediately)
table1DAO.setTable1SeqVal(currTable1SeqVal + entries.size());
table2DAO.setTable2SeqVal(currTable2SeqVal + entries.size());
for(int i = 0; i < entries.size(); i++) {
// Prepare Domain object...
UsersT user = new User();
user.setID(currTable1SeqVal + 1 + i); // Set ID manually
user.setCreatedDate(new Date());
// etc.
StudyParticipantsT sp = new StudyParticipantsT();
sp.setID(currTable2SeqVal + 1 + i); // Set ID manually
// etc.
user.setStudyParticipant(sp);
// Add to Batch-Insert List
batchInsertUsers.add(user);
// If list size ready for Batch-Insert (in this ex. 1000), or if at the end of all subjectIds, perform Batch Insert (both tables) and clear list
if (batchInsertUsers.size() == 1000 || i == subjectIds.size() - 1) {
// Part 1: Insert batch into USERS_T
nativeBatchInsertUsers(jdbcTemplate, batchInsertUsers);
// Part 2: Insert batch into STUDY_PARTICIPANTS_T
nativeBatchInsertStudyParticipants(jdbcTemplate, batchInsertUsers);
// Reset list
batchInsertUsers.clear();
}
}
}
, тогда ваши субметоды пакетной вставки, указанные выше:
1)
private void nativeBatchInsertUsers(JdbcTemplate jdbcTemplate, final List<UsersT> batchInsertUsers) {
String sqlInsert = "INSERT INTO PUBLIC.USERS_T (id, password, ... )"; // etc.
jdbcTemplate.batchUpdate(sqlInsert, new BatchPreparedStatementSetter() {
@Override
public int getBatchSize() {
return batchInsertUsers.size();
}
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setInt(1, batchInsertUsers.get(i).getId()); // ID (provided by ourselves)
ps.setDate(2, batchInsertUsers.get(i).getCreatedDate());
//etc.
}
});
}
2)
private void nativeBatchInsertStudyParticipants(JdbcTemplate jdbcTemplate, final List<UsersT> batchInsertUsers) {
String sqlInsert = "INSERT INTO PUBLIC.STUDY_PARTICIPANTS_T (id, ... )"; // etc.
jdbcTemplate.batchUpdate(sqlInsert, new BatchPreparedStatementSetter() {
@Override
public int getBatchSize() {
return batchInsertUsers.size();
}
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setInt(1, batchInsertUsers.get(i).getStudyParticipants().getId()); // ID (provided by ourselves)
//etc.
}
});
}
Есть способы чтобы получить / установить значения последовательности, например, в Postgres это
SELECT last_value FROM users_t_id_seq; -- GET SEQ VAL
SELECT setval('users_t_id_seq', 621938); -- SET SEQ VAL
Обратите внимание, что все находится под @Transactional
. Если в методе есть какие-либо исключения, все данные откатываются (для всех исключений rollbackFor = Exception.class
). Единственное, что не откатывается - это ручное обновление последовательности. Но это нормально, последовательности могут иметь пробелы.
2) Другое решение, если вы хотите опуститься до уровня PreparedStatement
, это Statement.RETURN_GENERATED_KEYS
:
PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)
После вы выполняете ps
, ResultSet
будет содержать ваши идентификаторы в порядке их создания. Вы можете перебрать ResultSet и сохранить идентификаторы в отдельном списке.
while (rs.next()) {
generatedIDs.add(rs.getInt(1));
}
Помните, что в этом случае вы несете ответственность за собственное управление транзакциями. Вам нужно conn.setAutoCommit(false);
, чтобы накапливать партии без реальной настойчивости, а затем conn.commit();
/ conn.rollback();
.