如何使用jOOQ RecordUnmapper?

问题描述 投票:0回答:1

我正在尝试实现一个jOOQ RecordUnmapper来调整我稍后将插入/更新的记录。

我的尝试如下,问题是Record类无法实例化。如何创建Record对象?另外,如何在插入/更新中使用取消映射?

public class TableUnmapper implements RecordUnmapper<Table, Record> {

    @Override
    public Record unmap(Table t) throws MappingException {
        Record r = new Record();  // <-- this line does not compile
        r.from(t);
        r.set(TABLES.TITLE_FONT_FAMILY, t.getTitleFont().getFontFamily());
        return r;
    }

}
java sql jooq
1个回答
2
投票

Record是一个接口,所以你不能直接创建一个接口的实例,你必须实例化一个实现Record接口的类,如果你使用Jooq代码生成器,你可能已经有了一个TableRecord类,这就是你的类可以用于这个purpouse,

那么unmapper应该看起来像:

public class TableUnmapper implements RecordUnmapper<Table, TableRecord> {

    @Override
    public TableRecord unmap(Table t) throws MappingException {

        TableRecord r = new TableRecord(t.getSomeAttribute());

        r.setAttribute(t.getSomeOtherAttribute());

        return r;
    }

}

要使用取消映射:

DSLContext create;
Table table = new Table(/* Whatever Arguments */);
TableUnmapper unmapper = new TableRecordUnmapper();

// Insert
create.insertInto(TABLES).set(unmapper.unmap(table));

// update
create.executeUpdate(unmapper.unmap(table));
© www.soinside.com 2019 - 2024. All rights reserved.