Best way to copy entity and it's related entities?

Hi, all. Sorry for yet another long post…

I have a Customer entity (BaseLongIdEntity) with a number of related entities. I want to clone a customer record. If I use the deepCopy() method, which I believe copies my base entity and all related entities, my new Customer has the same ID as my old customer.

How do I get my new Customer to have a new ID - AND - how do I get all the related entities to point to that new ID?

Here is an excerpt from my Customer class showing how my sequence generation is set up. The app is a rewrite of an Oracle Forms 6i app, so I have to use what is in the database.

public class Customers extends BaseLongIdEntity {

    @Id
    @SequenceGenerator(schema="SBOWNER", name="IDGEN", sequenceName = "IDGEN", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "IDGEN")
    @Column(name="ID")
    protected Long id;
     .
     .
     .
    @OrderBy("directory asc")
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "cus")
    protected List<Mergedeliv> mergedeliv;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "cus")
    protected List<Repdefs> repdefs;

Excerpt from mergedeliv:

public class Mergedeliv extends BaseLongIdEntity {

    @Id
    @SequenceGenerator(schema="SBOWNER", name="IDGEN", sequenceName = "IDGEN", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "IDGEN")
    @Column(name="ID")
    protected Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CUS_ID")
    protected Customers cus;

And, worse yet, Composite key in Repdefs (excerpt):

@Embeddable
public class RepdefsCompKey extends EmbeddableEntity {

    @Column(name = "CUS_ID")
    protected Long cus;
    @Column(name = "\"FUNCTION\"")
    protected Long function;
    @Column(name="REPNUM")
    protected Long repnum;
public class Repdefs extends BaseGenericIdEntity<RepdefsCompKey> {

    @EmbeddedId
    protected RepdefsCompKey id;

    @Lookup(type = LookupType.DROPDOWN)
    @MapsId("cus")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="CUS_ID")
    protected Customers cus;

So, the goal is to make a new Customer that is a copy of the original customer, copy all the related entities, and end up with the new related entities linked to the new customer entity.

Do I have to hard-code the whole thing? There are a LOT of fields and entities.

Hi,

You don’t need to hard-code the copying code.

CUBA provides entity metadata which allows you to traverse entity graph, entities properties and reflectively read and write attribute values.

The best way for you is to copy MetadataUtils#deepCopy code, analyze it and modify it to exactly suit your requirements.

One trick you might use - is to introduce your own entity / attribute annotations and use them to mark properties to alter copying logic.

Attribute annotations will be available in runtime by invoking MetaProperty#getAnnotations() method.

OK, thank you.