批量與歷史
區分適用於一般保存的保存生命週期與批量插入/upsert、移除備份、歷史快照的行為差異。
save / saveAll
save(entity)
如果沒有 id,則使用 insertOne,如果有 id,則根據 _id 使用 replaceOne(..., upsert=true)。
saveAll(...)
對每個實體執行 save(),因此會應用 beforePersist/afterPersist 生命週期鉤子。
批量 API 選擇
saveAllBulk
基於 insertMany 的批量插入。這不是更新現有 id 的 API。
saveAllBulkUpsert
如果實體 id 不存在則插入,存在則執行 _id 過濾 + $set + upsert。
saveAllBulkUpsertByKey
根據業務複合鍵進行 upsert。如果任一鍵缺失,則該實體將被視為插入。
實體刪除和移除備份
backup을 활성화하면 <sourceCollection>_remove collection을 사용합니다. delete와 backup을 하나의 원자 작업으로 묶어야 한다면 호출 측에서 getTxJob(...) transaction 범위를 잡습니다.
歷史快照
createHistory는 현재 MongoExecutionContext.write(...) 결과를 deep copy하고 _id를 제거한 뒤 <sourceCollection>_history 또는 지정한 prefix collection에 insert합니다. history write도 save lifecycle hook을 호출하지 않습니다.