Cloning a Java bean with few of the properties values - BeanUtils

642 Views Asked by At

I am having a Java bean, which i want to clone it to get a new instance, but in the new instance, I want few of the properties to have values, others i don't want to be cloned/copied.

Example:-

Source:- Class ABC, Object src --> field1 = 10, field2 =5, field3 = 6, field4 = 20
Target:- Class ABC, Oject clone --> field1 = 10, field2 = 5, field3 = null, field4 = null

I can also use copyProperties but than, I have to create a similar but small class structure.

I can also use copyProperty but then, I have to write it multiple times.

Is there a good solution to this.

1

There are 1 best solutions below

0
On

Use this method copyProperties(Object dest, Object orig) Copy property values from the origin bean to the destination bean for all cases where the property names are the same.

You have mentioned you are doing same.May you go to article.I always using for my spring web-app may be it also helpful to you.Comment if it solves your problem.

http://java.dzone.com/articles/how-copy-bean-properties