How to use jOOQ RecordUnmapper?

ぐ巨炮叔叔 提交于 2019-12-11 14:55:20

问题


I'm trying to implement a jOOQ RecordUnmapper to adjust a record that I will later insert/update.

My attempt below, problem is that the Record class cannot be instantiated. How to create the Record object? Also, how to use the unmapper in insert/update?

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;
    }

}

回答1:


Record is an interface, so you can't directly create an instance of the interface, you have to instantiate a class that implements the Record interface, if you use Jooq code generator, you probably already have a TableRecord class, this is the class you can use for this purpouse,

then the unmapper should look something like:

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;
    }

}

To use the unmapper:

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));


来源:https://stackoverflow.com/questions/54798190/how-to-use-jooq-recordunmapper

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!