Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] Application Developer Responsibilities when overriding java.lang.Object.clone() in EclipseLink JPA mapped classes?

Hello everyone,

 

I didn’t find any information on the wiki on this topic, and that may simply be due to no change in the contract regarding how overriding implementations of clone() should be implemented in EclipseLink JPA mapped classes. However, I am looking for confirmation of that.

 

Suppose I have a JPA mapped class, with relationships to other JPA mapped classes as follows:

 

@Entity

public class Container extends Object

{

    @OneToOne

    private A a;

 

    @ManyToOne

    private B b;

 

    @OneToMany

    private List<C> cs;

 

    @ManyToMany

    private List<D> ds;

}

 

Now suppose I wish to create instances of Container by cloning persistent instances. What I am looking for are any differences in the overriding clone() implementation if the Container class were defined as above, or if the Container class was not JPA mapped and had none of the above annotations. Would the following method suffice within the JPA mapped Container class, assuming the overriding implementation of clone() on the JPA mapped A, B, C, and D classes were equally appropriate:

 

@Override

public Object clone()

{

    Container result = (Container) super.clone();

 

    result.a = (A) this.a.clone();

    result.b = (B) this.b.clone();

 

    result.cs = new ArrayList<C>(this.cs.size());

    for (C sourceC : this.cs)

    {

        C cloneC = (C) sourceC.clone();

        result.cs.add(cloneC);

        cloneC.setContainer(result);

    }

 

    result.ds = new ArrayList<D>(this.ds.size());

    for (D sourceD : this.ds)

    {

        D cloneD = (D) sourceD.clone();

        result.ds.add(cloneD);

        cloneD.setContainer(result);

    }

}

 

If not, what differences in the above implementation would be recommended for use in the JPA mapped Container class?

 

Thanks,

 

Doug

 






The contents of this electronic mail message and any attachments are confidential, possibly privileged and intended
for the addressee(s) only. Only the addressee(s) may read, disseminate, retain or otherwise use this message. If
received in error, please immediately inform the sender and then delete this message without disclosing its contents
to anyone.

Back to the top