我正在嘗試使用記錄執行upsert(使用3.10快照),並且注意到當我使用addRecord(R)
時,更新的set
數據未被添加到查詢中。在DAO我:PostgreSQL Upsert使用記錄
try (Connection writeConn = DatabaseManager.getConnection(true);
final DSLContext writeContext = DatabaseManager.getBuilder(writeConn);
InsertQuery<UserProgramAssignmentsRecord> programAssignmentsUpdateQuery = writeContext
.insertQuery(AccessControlWriteDAO.userProgramAssignments)) {
programAssignmentsUpdateQuery.onDuplicateKeyUpdate(true);
programAssignments.forEach(pa -> programAssignmentsUpdateQuery.addRecord(pa.toRecord()));
if (!programAssignments.isEmpty()) {
if (AccessControlWriteDAO.logger.isInfoEnabled()) {
AccessControlWriteDAO.logger.info(writeContext.renderInlined(programAssignmentsUpdateQuery));
}
// programAssignmentsUpdateQuery.execute();
}
} catch (final SQLException e) {
throw new DAOException(e.getMessage(), e);
}
裏面的類programAssignments的,我有:
/**
* <p>
* This method turns the object into a {@link Record} that can then be attached to a {@link Connection} and
* {@link DSLContext}.
* </p>
*
* @return A usable {@link UserProgramAssignmentsRecord}
*/
public UserProgramAssignmentsRecord toRecord() {
UserProgramAssignmentsRecord upar = new UserProgramAssignmentsRecord();
getAdministerProgram().ifPresent(upar::setAdminister);
getCreateProjects().ifPresent(upar::setCreateProjects);
getJustification().ifPresent(upar::setJustification);
getProgramId().ifPresent(upar::setProgramId);
getUserId().ifPresent(upar::setUserId);
getViewRevisions().ifPresent(upar::setViewRevisions);
return upar;
}
的代碼產生以下查詢:
insert into "%%removed%%"."user_project_assignments" (
"project_id",
"edit_draft",
"justification",
"user_id"
)
values (
98332,
true,
'The user created the project',
675
)
on conflict (
"project_id",
"user_id"
) do update
set [ no fields are updated ]
我必須切換到使用addValue(Field<T>,T)
,addValueForUpdateField(Field<T>,T)
和newRecord?我會想到傳遞記錄將是等效的,但似乎並不如此,因爲addRecord(R)
似乎沒有設置字段來更新 - 或者 - 我在做我的toRecord()
方法有問題嗎? InsertQuery
對象是否有某種addRecordForUpdate(R)
?這對多行upserts(這是我想要做的)有多好?