Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #32931 > unrolled thread

What goes wrong with cloning here

Started byCecil Westerhof <Cecil@decebal.nl>
First post2015-10-23 17:49 +0200
Last post2015-10-24 01:44 +0200
Articles 4 — 2 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  What goes wrong with cloning here Cecil Westerhof <Cecil@decebal.nl> - 2015-10-23 17:49 +0200
    Re: What goes wrong with cloning here Tassilo Horn <tsdh@gnu.org> - 2015-10-23 18:12 +0200
      Re: What goes wrong with cloning here Cecil Westerhof <Cecil@decebal.nl> - 2015-10-24 00:51 +0200
        Re: What goes wrong with cloning here Cecil Westerhof <Cecil@decebal.nl> - 2015-10-24 01:44 +0200

#32931 — What goes wrong with cloning here

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-10-23 17:49 +0200
SubjectWhat goes wrong with cloning here
Message-ID<87oafplh3t.fsf@Equus.decebal.nl>

[Multipart message — attachments visible in raw view] — view raw

I need to use Cloneable. (I know that some people say you should never
use it, but it is a requirement of the project.)

I made something I thought should work, but it does not. I have
attached my dumbed down code. What am I doing wrong?

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.

[toc] | [next] | [standalone]


#32932

FromTassilo Horn <tsdh@gnu.org>
Date2015-10-23 18:12 +0200
Message-ID<87pp05k1h5.fsf@gnu.org>
In reply to#32931
Cecil Westerhof <Cecil@decebal.nl> 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.

Bye,
Tassilo

[toc] | [prev] | [next] | [standalone]


#32933

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-10-24 00:51 +0200
Message-ID<87k2qdkxlh.fsf@Equus.decebal.nl>
In reply to#32932
On Friday 23 Oct 2015 18:12 CEST, Tassilo Horn wrote:

> Cecil Westerhof <Cecil@decebal.nl> 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?

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [prev] | [next] | [standalone]


#32934

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-10-24 01:44 +0200
Message-ID<87fv11kv4n.fsf@Equus.decebal.nl>
In reply to#32933

[Multipart message — attachments visible in raw view] — view raw

On Saturday 24 Oct 2015 00:51 CEST, Cecil Westerhof wrote:

> On Friday 23 Oct 2015 18:12 CEST, Tassilo Horn wrote:
>
>> Cecil Westerhof <Cecil@decebal.nl> 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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web