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