问题描述
我正在尝试实现一个jOOQ RecordUnmapper
来调整一条记录,稍后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;
}
}
1楼
Record
是一个接口,因此您不能直接创建该接口的实例,您必须实例化一个实现Record
接口的类,如果您使用Jooq代码生成器,则可能已经有一个TableRecord
类,这就是您所需要的类可以用于这个目的
那么解映射器应类似于:
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));