Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.java.programmer Subject: Re: What goes wrong with cloning here Date: Sat, 24 Oct 2015 01:44:40 +0200 Organization: Decebal Computing Lines: 187 Message-ID: <87fv11kv4n.fsf@Equus.decebal.nl> References: <87oafplh3t.fsf@Equus.decebal.nl> <87pp05k1h5.fsf@gnu.org> <87k2qdkxlh.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: mx02.eternal-september.org; posting-host="528adfd6ad074c92fdc6a7f8fb9e23d8"; logging-data="3271"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+3lM8NQwz764FDwvTY3Tx+/O2LCF/AN+I=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) X-Homepage: http://www.decebal.nl/ Cancel-Lock: sha1:2BGn1SsbbEb8oXLwh5hm0gJhMr8= sha1:1nlA9vyPwTpWTEEEY+eBailBZFo= Xref: csiph.com comp.lang.java.programmer:32934 --=-=-= Content-Type: text/plain On Saturday 24 Oct 2015 00:51 CEST, Cecil Westerhof wrote: > On Friday 23 Oct 2015 18:12 CEST, Tassilo Horn wrote: > >> Cecil Westerhof writes: >> >>> When I in class A change a field from class B in the clone, the >>> field in the original is also changed. That should not happen. >>> >>> public class A implements Cloneable { >>> private B key; >>> ^^^ >>> public Object clone() throws CloneNotSupportedException { >>> A clone = (A) super.clone(); >>> B newKey = new B(); >>> B oldKey = getKey(); >>> >>> newKey.setA(oldKey.getA()); >>> newKey.setB(oldKey.getB()); >>> newKey.setC(oldKey.getC()); >>> newKey.setD(oldKey.getD()); >>> clone.setKey(key); >> ^^^ >> >> I guess here you want to set the key of the clone to newKey, not to >> this.key. > > Was an error in my dumbing down. :'-( In the original program it was > done correctly. > > > I found the problem. It is an application where the class is a JPA > class. When doing the test (in the original program) with an entity > that was gotten by a read it goes wrong. When doing it with an > entity gotten by a new it does what I expect. > > Maybe it has to do with the fact that key is the primary key? For if someone is interested. I rewrote the code. I made the class B Cloneable again. (Was like this before my first post, but I was thinking that maybe there went something wrong.) When class B changes class A does not need to change. Also added hashCode to toString. --=-=-= Content-Type: text/x-java-source Content-Disposition: attachment; filename=A.java public class A implements Cloneable { //public public static void main(String[] args) throws CloneNotSupportedException { A clone, original; B key; original = new A(); key = new B(); key.setA("Some text"); key.setB(12); key.setC(73); original.setKey(key); clone = (A) original.clone(); clone.getKey().setB(99); System.out.println(original.getKey()); System.out.println(clone.getKey()); } public A() { } public Object clone() throws CloneNotSupportedException { A clone = (A) super.clone(); B oldKey = getKey(); B newKey = (B) oldKey.clone(); clone.setKey(newKey); return clone; } public B getKey() { return key; } public void setKey(B key) { this.key = key; } public String getC() { return c; } public String getD() { return d; } public void setC(String c) { this.c = c; } public void setD(String d) { this.d = d; } // private private B key; private String c; private String d; } --=-=-= Content-Type: text/x-java-source Content-Disposition: attachment; filename=B.java import java.io.Serializable; import java.util.Date; public class B implements Cloneable { // public public B() { } public Object clone() throws CloneNotSupportedException { return super.clone(); } public String getA() { return a; } public int getB() { return b; } public int getC() { return c; } public Date getD() { return d; } public void setA(String a) { this.a = a; } public void setB(int b) { this.b = b; } public void setC(int c) { this.c = c; } public void setD(Date d) { this.d = d; } public String toString() { return String.format("B@%s [a: %s, b: %s, c: %s, d: %s]", Integer.toHexString(hashCode()), a, b, c, d); } // private private String a; private int b; private int c; private Date d; } --=-=-= Content-Type: text/plain -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof --=-=-=--