In our application I have a routine that manually iterates over the properties of an POJO loaded by Hibernate and assigns those properties to a new instance of that object and then saves it.
e.g
Class TestClass{
Integer a;
String b;
public void setA(Integer a){
this.a = a;
}
public Integer getA(){
return a;
}
...
}
in the action it does
TestClass objectOne = testDao.get(id);
TestClass objectTwo = new TestClass();
and then the iteration is like
objectOne.setA(objectTwo.getA());
objectOne.setB(objectTwo.getB());
I’m wondering if there is a more efficient way to iterate through each propertie of objectOne and set it to ObjectTwo, because in our real application those objects have around 20 properties or so.
Thanks a lot for your help.
Sure. Check out the Apache Commons BeanUtils lib. There you can use
copyProperties(Object dest, Object orig)