2017-09-14 35 views
2

我想使用jooq使用batchStore插入記錄。我需要知道我們如何可以更新唯一約束的記錄,目前它是扔 該記錄已經存在批量存儲jooq上的唯一約束更新記錄

SQL Error [23505]: ERROR: duplicate key value violates unique constraint 

下面是代碼

DSLContext create = getDSLContext(); 
List<UserRecord> userRecordList = new ArrayList<>(); 
for (Users user : model.getUsers()) { 
    User record = create.newRecord(user); 
    userRecordList.add(record); 
} 
create.batchStore(userRecordList).execute(); 

目前,它插入記錄異常罰款,但當重複記錄發現基於獨特的約束時,它應該更新記錄

+0

事實上,'batchStore()'命令不執行一個SQL'MERGE'語句語義,但快捷鍵調用'UpdatableRecord.store()'在每個單獨的記錄,運行'店()'命令中批量。有一項改進文檔的功能請求:https://github.com/jOOQ/jOOQ/issues/6584 –

回答

1

我已經解決了這個問題,通過使用通用接口UpdatableRecord,首先我已經使用fetchOne()查詢來獲取rec ord的唯一約束的基礎上,將給出UpdatableRecord,然後設置與更新的值然後將其添加到userRecordlist這將進一步傳入批處理

0

我必須做同樣的JN_newbie。爲了完整,這裏是我的解決方案。

// OpeningtimeRecord is the JOOG generated record for the Openingtime table. 
List<OpeningtimeRecord> openingRecords = new ArrayList<>(); 

// Openingtime is the POJO generated from the database schema for the table. 
for (Openingtime opening : openings) 
{ 
    // Check to see if this record already exists in the database 
    OpeningtimeRecord record = dbContext.fetchOne(*TABLE*, *TABLE.ID*.eq(opening.getId())); 

    // If it doesn't exist then we need to create a new record 
    if (null == record) 
    { 
     record = dbContext.newRecord(*TABLE*, opening); 
    } 
    else 
    { 
     // Update the record with any new data from the POJO. 
     record.from(opening); 
    } 

    openingRecords.add(record); 
} 

int[] results = this.dbContext.batchStore(openingRecords).execute();