diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractRecord.java b/jOOQ/src/main/java/org/jooq/impl/AbstractRecord.java index a7b8ccaf2d..fdf83cb7f2 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractRecord.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractRecord.java @@ -522,48 +522,53 @@ abstract class AbstractRecord extends AbstractStore implements Record { } @Override - public final R into(final Table table) { - return Utils.newRecord(table, configuration()) - .operate(new RecordOperation() { + public final R into(Table table) { + return Utils.newRecord(table, configuration()).operate(new RecordCopyOperation()); + } - @Override - public R operate(R record) throws MappingException { - try { - for (Field targetField : table.fields()) { - Field sourceField = field(targetField); + final R intoRecord(Class type) { + return Utils.newRecord(type, fields(), configuration()).operate(new RecordCopyOperation()); + } - if (sourceField != null) { - Utils.setValue(record, targetField, AbstractRecord.this, sourceField); + private class RecordCopyOperation implements RecordOperation { + + @Override + public R operate(R record) throws MappingException { + try { + for (Field targetField : record.fields()) { + Field sourceField = field(targetField); + + if (sourceField != null) { + Utils.setValue(record, targetField, AbstractRecord.this, sourceField); + } + } + + // [#1522] If the primary key has been fully fetched, then changed + // flags should all be reset in order for the returned record to be + // updatable using store() + if (record instanceof AbstractRecord) { + UniqueKey key = ((AbstractRecord) record).getPrimaryKey(); + + if (key != null) { + boolean isKeySet = true; + + for (Field field : key.getFields()) { + isKeySet = isKeySet && (field(field) != null); + } + + if (isKeySet) { + record.changed(false); } } - - // [#1522] If the primary key has been fully fetched, then changed - // flags should all be reset in order for the returned record to be - // updatable using store() - if (record instanceof AbstractRecord) { - UniqueKey key = ((AbstractRecord) record).getPrimaryKey(); - - if (key != null) { - boolean isKeySet = true; - - for (Field field : key.getFields()) { - isKeySet = isKeySet && (field(field) != null); - } - - if (isKeySet) { - record.changed(false); - } - } - } - - return record; - // All reflection exceptions are intercepted - } - catch (Exception e) { - throw new MappingException("An error ocurred when mapping record to " + table, e); } + + return record; + // All reflection exceptions are intercepted } - }); + catch (Exception e) { + throw new MappingException("An error ocurred when mapping record to " + record, e); + } + } } @Override diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultRecordMapper.java b/jOOQ/src/main/java/org/jooq/impl/DefaultRecordMapper.java index 28a2b726a7..f49f0ca619 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultRecordMapper.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultRecordMapper.java @@ -235,6 +235,12 @@ public class DefaultRecordMapper implements RecordMapper) new RecordToRecordMapper(); + return; + } + // [#1340] Allow for using non-public default constructors try { delegate = new MutablePOJOMapper(type.getDeclaredConstructor()); @@ -352,6 +358,26 @@ public class DefaultRecordMapper implements RecordMapper { + + @Override + public final AbstractRecord map(R record) { + try { + if (record instanceof AbstractRecord) { + return ((AbstractRecord) record).intoRecord((Class) type); + } + + throw new MappingException("Cannot map record " + record + " to type " + type); + } + catch (Exception e) { + throw new MappingException("An error ocurred when mapping record to " + type, e); + } + } + } + /** * Convert a record into a mutable POJO type *