U can use Apache BeanUtils ...
Setting same properites POJO in java
質問
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.?
解決
他のヒント
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;
}
所属していません StackOverflow