U can use Apache BeanUtils ...
Setting same properites POJO in java
Frage
I have two POJO with same setter and getter now i want to copy all the bean value to another bean. What can be the best way to copy all the information of one POJO to another.?
Lösung
Andere Tipps
Implement deep copy here. It will be best as all sub POJO's inside POJO will also be copied and there will be no loss.
public CloneExample deepCopy() {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
return (CloneExample) ois.readObject();
} catch (IOException e) {
return null;
} catch (ClassNotFoundException e) {
return null;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow