我正在尝试实现一个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;
}
}
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));