2017-09-14 114 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();