Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copying all properties of one pojo to another in java?

Tags:

java

java-ee-6

I have below POJO of some third party jar which we cannot expose to our clients directly.

ThirdPartyPojo.java

public class ThirdPartyPojo implements java.io.Serializable {

    private String name;
    private String ssid;
    private Integer id;

    //public setters and getters

}

Above class is part of third party jar which we are using as below.

ThirdPartyPojo result = someDao.getData(String id);

Now our plan is as ThirdPartyPojo is part of third party jar, we cannot send ThirdPartyPojo result type directly to clients. we want to create our own pojo which will have same properties as ThirdPartyPojo.java class. we have to set the data from ThirdPartyPojo.java to OurOwnPojo.java and return it as 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 know if there is a best way to have same properties in OurOwnPojo.java as ThirdPartyPojo.java and populate the data from ThirdPartyPojo.java to OurOwnPojo.java and return the same?

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!

like image 322
user755806 Avatar asked Nov 18 '13 05:11

user755806


People also ask

How do I convert one bean to another bean in Java?

Use PropertyUtils. copyProperties() to copy the properties from one bean to another. The first parameter is the destination bean, and the second parameter is the bean to copy properties from: import org.

Can POJO have methods?

A POJO has no naming convention for our properties and methods. This class can be used by any Java program as it's not tied to any framework.

Does POJO provide complete control of members?

The POJO class does not impart substantial control over the members. The Java Beans do have complete control over the members. The POJO class can be used to implement serializable interfaces. The Java Beans must implement serializable interfaces.


2 Answers

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

Task subTask = new Task();
org.springframework.beans.BeanUtils.copyProperties(subTaskInfo.getTask(), subTask);
like image 187
feuyeux Avatar answered Sep 17 '22 13:09

feuyeux


Probably you are searching 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;
}
like image 29
Masudul Avatar answered Sep 19 '22 13:09

Masudul