Java/Spring JDBC:批量插入到2个表中:从第2个表所需的第一批插入中获取FK ID
我正在使用Java/Spring JDBC:批量插入到2个表中:从第2个表所需的第一批插入中获取FK ID,java,spring,spring-jdbc,jdbctemplate,batch-insert,Java,Spring,Spring Jdbc,Jdbctemplate,Batch Insert,我正在使用jdbcTemplate批量插入到两个表中。第一个表很简单,有一个ID。第二个表有一个FK参考用户ID,我需要在插入前从表1中获取该参考 假设我有这个: 主要Java代码(这里我分成了几个批下面是答案 1) 如果您使用的是jdbcTemplate(springjdbc),一个解决方案是提前保留您自己的ID范围。然后自己为每一行提供手动计算的ID。例如 @Transactional(readOnly = false, rollbackFor = Exception.class) publ
jdbcTemplate
批量插入到两个表中。第一个表很简单,有一个ID
。第二个表有一个FK参考用户ID
,我需要在插入前从表1中获取该参考
假设我有这个:
主要Java代码(这里我分成了几个批下面是答案
1) 如果您使用的是jdbcTemplate
(springjdbc),一个解决方案是提前保留您自己的ID范围。然后自己为每一行提供手动计算的ID。例如
@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();
}
}
}
还要注意,所有内容都在@Transactional
下。如果该方法中存在任何异常,则会回滚所有数据(对于all异常,rollboor=Exception.class
)。唯一没有回滚的是手动序列更新。但没关系,序列可能有间隙
2) 另一个解决方案是,如果您愿意下拉到PreparedStatement
级别,则为语句。返回生成的\u键
:
PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)
执行ps
后,ResultSet
将按照创建ID的顺序包含ID。您可以迭代结果集并将ID存储在单独的列表中
while (rs.next()) {
generatedIDs.add(rs.getInt(1));
}
记住,在这种情况下,您负责自己的事务管理。您需要conn.setAutoCommit(false)
让批在没有真正持久性的情况下堆积起来,然后conn.commit()代码>/连接回滚()代码>以下是答案
1) 如果您使用的是jdbcTemplate
(springjdbc),一个解决方案是提前保留您自己的ID范围。然后自己为每一行提供手动计算的ID。例如
@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();
}
}
}
还要注意,所有内容都在@Transactional
下。如果该方法中存在任何异常,则会回滚所有数据(对于all异常,rollboor=Exception.class
)。唯一没有回滚的是手动序列更新。但没关系,序列可能有间隙
2) 另一个解决方案是,如果您愿意下拉到PreparedStatement
级别,则为语句。返回生成的\u键
:
PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)
执行ps
后,ResultSet
将按照创建ID的顺序包含ID。您可以迭代结果集并将ID存储在单独的列表中
while (rs.next()) {
generatedIDs.add(rs.getInt(1));
}
记住,在这种情况下,您负责自己的事务管理。您需要conn.setAutoCommit(false)
让批在没有真正持久性的情况下堆积起来,然后conn.commit()代码>/连接回滚()代码>
SELECT last_value FROM users_t_id_seq; -- GET SEQ VAL
SELECT setval('users_t_id_seq', 621938); -- SET SEQ VAL
PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)
while (rs.next()) {
generatedIDs.add(rs.getInt(1));
}