我需要将数据批量插入2个表中,并且Table2具有来自Table1的FK ID,因此我需要获取刚刚插入到Table1中每个Table2行的ID。
表1 =USERS_T
Table2 = STUDY_PARTICIPANTS_T
具有FK:USER_ID = USERS_T.ID
我正在使用jdbcTemplate.batchUpdate()
从Java应用程序执行此操作。
问题
假设我正在使用Postgres的currval('users_t_id_seq')
来获取Table1使用的序列值。
插入到2个表中的一种方法是顺序的,但这将不起作用,因为currval
仅具有最新行的ID,因此所有Table2插入都将被弄乱。
// Part 1: Insert into USERS_T
batchInsertUsers(jdbcTemplate, batchInsertUsers);
// Part 2: Insert into STUDY_PARTICIPANTS_T
batchInsertStudyParticipants(jdbcTemplate, batchInsertUsers);
private void batchInsertUsers(JdbcTemplate jdbcTemplate, final List<UsersT> batchInsertUsers) {
String sql = "INSERT INTO USERS_T (ID, .., ..) " +
"VALUES (nextval('users_t_id_seq'), .., ..";
jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
//...
});
}
private void batchInsertStudyParticipants(JdbcTemplate jdbcTemplate, final List<UsersT> batchInsertUsers) {
String sql = "INSERT INTO STUDY_PARTICIPANTS_T (ID, USER_ID, ..) " +
"VALUES (nextval('study_participants_t_id_seq'),
currval('users_t_id_seq'), .., ..";
jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
//...
});
}
捕获”最新ID的另一种方法是对两个表进行多行插入,如下所示-这样我就可以捕获最新的USERS_T
序列值。
但是这会出现以下错误:
PSQLException: Too many update results were returned.
[当我使用;
分隔我的1行插入内容时发生此错误,并记录在这里https://www.postgresql.org/message-id/[email protected]
private void batchInsertAll(JdbcTemplate jdbcTemplate, final List<UsersT> batchInsertUsers) {
String sql = "INSERT INTO USERS_T (ID, .. ) " +
"VALUES (nextval('users_t_id_seq'), ..) " +
";" + // Note semicolon separating my multiline SQL
"INSERT INTO STUDY_PARTICIPANTS_T (ID, USER_ID, ..) " +
"VALUES (nextval('study_participants_t_id_seq'),
currval('users_t_id_seq'), .., ..";
jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
//...
});
}
因此,在此Postgres Batch-Insert过程中,我找不到一种方法来进行两张表插入并捕获适当的Table1 ID。
这是答案。
1]如果使用jdbcTemplate(Spring JDBC),一种解决方案是预先保留自己的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();
}
}
}
然后再使用上面引用的Batch-Insert子方法:
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后,结果集将按照创建顺序包含您的ID。您可以遍历ResultSet并将ID存储在单独的列表中。
while (rs.next()) {
generatedIDs.add(rs.getInt(1));
}
[请记住,在这种情况下,您要负责自己的事务管理。您需要conn.setAutoCommit(false);
以使批处理堆积而没有真正的持久性,然后单击conn.commit(); / conn.rollback();
。