gpt4 book ai didi

Java/Spring JDBC : Batch Insert into 2 Tables: Obtain FK ID from 1st Batch Insert Required for 2nd Table

转载 作者:太空宇宙 更新时间:2023-11-04 09:04:30 25 4
gpt4 key购买 nike

我正在使用jdbcTemplate批量插入到2个表中。第一个表很简单,并且有一个 ID。第二个表有一个 FK 引用 USER_ID,我需要在插入之前从表 1 中获取它。

假设我有这个:

主要Java代码(这里我分成批处理<= 1000)

for(int i = 0; i < totalEntries.size(); i++) {
// Add to Batch-Insert List; if list size ready for batch-insert, or if at the end, batch-persist & clear list
batchInsert.add(user);

if (batchInsert.size() == 1000 || i == totalEntries.size() - 1) {
// 1. Batch is ready, insert into Table 1
nativeBatchInsertUsers(jdbcTemplate, batchInsert);
// 2. Batch is ready, insert into Table 2
nativeBatchInsertStudyParticipants(jdbcTemplate, batchInsert);
// Reset list
batchInsert.clear();
}
}

批量插入表 1 的方法(注意我在此处获取 USERS_T 的 Seq Val)

    private void nativeBatchInsertUsers(JdbcTemplate jdbcTemplate, final List<UsersT> batchInsert) {

String sqlInsert_USERS_T = "INSERT INTO PUBLIC.USERS_T (id, password, user_name) " +
"VALUES (nextval('users_t_id_seq'), ?, ? " +
")";

// Insert into USERS_T using overridden JdbcTemplate's Native-SQL batchUpdate() on the string "sqlInsert_USERS_T"
jdbcTemplate.batchUpdate(sqlInsert_USERS_T, new BatchPreparedStatementSetter() {

@Override
public int getBatchSize() {
return batchInsert.size();
}

@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setString(1, null);
ps.setString(2, batchInsert.get(i).getUsername());
// etc.
});
}

批量插入表2的方法

private void nativeBatchInsertStudyParticipants(JdbcTemplate jdbcTemplate, final List<UsersT> batchInsertUsers) {

String sqlInsert_STUDY_PARTICIPANTS_T =
"INSERT INTO PUBLIC.STUDY_PARTICIPANTS_T (id, study_id, subject_id, user_id) "
"VALUES (nextval('study_participants_t_id_seq'), ?, ?, ?
")";

// Insert into STUDY_PARTICIPANTS_T using overridden JdbcTemplate's Native-SQL batchUpdate() on the string "sqlInsert_USERS_T"
jdbcTemplate.batchUpdate(sqlInsert_STUDY_PARTICIPANTS_T, new BatchPreparedStatementSetter() {

@Override
public int getBatchSize() {
return batchInsert.size();
}

@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {

// PROBLEM: For Param #4, USER_ID, need to get the USERS_T.ID from Batch-Insert #1

}

});

}

当我进行第二批插入时,其中一列是返回到 USERS_T.ID 的 FK,称为 STUDY_PARTICIPANTS_T.USER_ID。我是否可以通过保留jdbcTemplate.batchUpdate()逻辑来获取它?

最佳答案

这就是答案。

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后,ResultSet将按照创建顺序包含您的ID。您可以迭代 ResultSet 并将 ID 存储在单独的列表中。

while (rs.next()) {
generatedIDs.add(rs.getInt(1));
}

请记住,在这种情况下,您要对自己的事务管理负责。您需要 conn.setAutoCommit(false); 让批处理堆积起来而不具有真正的持久性,然后 conn.commit();/conn.rollback();

关于Java/Spring JDBC : Batch Insert into 2 Tables: Obtain FK ID from 1st Batch Insert Required for 2nd Table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60379705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com