Copying all properties of one pojo to another in java?

I have a third-party bank below POJO, which we cannot provide to our customers directly.

ThirdPartyPojo.java

public class ThirdPartyPojo implements java.io.Serializable { private String name; private String ssid; private Integer id; //public setters and getters } 

The above class is part of a third party jar that we use, as shown below.

 ThirdPartyPojo result = someDao.getData(String id); 

Now our plan is that ThirdPartyPojo is part of a third-party jar, we cannot send results like ThirdPartyPojo directly to clients. we want to create our own pojo that will have the same properties as the ThirdPartyPojo.java class ThirdPartyPojo.java . we must set the data from ThirdPartyPojo.java to OurOwnPojo.java and return it as OurOwnPojo.java below.

 public OurOwnPojo getData(String id){ ThirdPartyPojo result = someDao.getData(String id) OurOwnPojo response = new OurOwnPojo(result); return response; //Now we have to populate above 'result' into **OurOwnPojo** and return the same. } 

Now I want to find out if there is a better way to have the same properties in OurOwnPojo.java as ThirdPartyPojo.java and fill in the data from ThirdPartyPojo.java in OurOwnPojo.java and return them?

 public class OurOwnPojo implements java.io.Serializable { private ThirdPartyPojo pojo; public OurOwnPojo(ThirdPartyPojo pojo){ this.pojo = pojo } //Now here i need to have same setter and getters as in ThirdPartyPojo.java //i can get data for getters from **pojo** } 

Thanks!

+7
java java-ee-6
source share
3 answers

You may be looking for Apache Commons BeanUtils.copyProperties .

 public OurOwnPojo getData(String id){ ThirdPartyPojo result = someDao.getData(String id); OurOwnPojo myPojo=new OurOwnPojo(); BeanUtils.copyProperties(myPojo, result); //This will copy all properties from thirdParty POJO return myPojo; } 
+11
source share

org.springframework.beans.BeanUtils is better than apache aone:

 Task subTask = new Task(); org.springframework.beans.BeanUtils.copyProperties(subTaskInfo.getTask(), subTask); 
0
source share

Do not confuse the source and end words:

 try { BeanUtils.copyProperties(new DestinationPojo(), originPojo); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } 
0
source share

All Articles