'What's the recommend way of cloning a jOOQ Record object?

It seems like you can't create a copy constructor, you can't make it implement cloneable.

I was thinking of using reflection to iterate over every attribute on the child class but is there a better way?



Solution 1:[1]

It seems like you can't create a copy constructor, you can't make it implement cloneable.

You can implement these things in the code generator easily yourself. In both cases, you will need to generate some custom code, and in the case of Cloneable, you will need to write a generator strategy (programmatic or configurative), which adds the Cloneable interface to all generated records.

If you're not using generated records, or if you want to clone any arbitrary record, you can still use one of the many Record.into() methods, which copy record contents into a new type.

Solution 2:[2]

To copy a record I have used the UpdatableRecord.copy() method.

/**
 * Duplicate this record (in memory) and reset all fields from the primary
 * key or main unique key, such that a subsequent call to {@link #store()}
 * will result in an <code>INSERT</code> statement.
 *
 * @return A new record, distinct from <code>this</code> record.
 */
R copy();

Solution 3:[3]

Using Jooq 3.16.5, a simple way to create a copy it to use the into(Field<?>... fields) method on the original instance, essentially copying all the fields into a new record:

import org.jooq.Record;

Record original = ...
Record copied = original.into(original.fields);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lukas Eder
Solution 2 Jose Martinez
Solution 3 Svend