在java中将一个pojo的所有属性复制到另一个?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20040800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:35:37  来源:igfitidea点击:

copying all properties of one pojo to another in java?

javajava-ee-6

提问by user755806

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

我有一些第三方 jar 的 POJO,我们不能直接向我们的客户公开。

ThirdPartyPojo.java

第三方Pojo.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.

上面的类是我们正在使用的第三方 jar 的一部分,如下所示。

ThirdPartyPojo result = someDao.getData(String id);

Now our plan is as ThirdPartyPojois part of third party jar, we cannot send ThirdPartyPojoresult type directly to clients. we want to create our own pojo which will have same properties as ThirdPartyPojo.javaclass. we have to set the data from ThirdPartyPojo.java to OurOwnPojo.javaand return it as below.

现在我们的计划是ThirdPartyPojo第三方 jar 的一部分,我们不能ThirdPartyPojo直接将结果类型发送给客户。我们想创建我们自己的 pojo,它将与ThirdPartyPojo.java类具有相同的属性。我们必须将来自 ThirdPartyPojo.java 的数据设置为OurOwnPojo.java并返回如下。

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.javaas ThirdPartyPojo.javaand populate the data from ThirdPartyPojo.javato OurOwnPojo.javaand return the same?

现在我想知道是否有最好的方法来在OurOwnPojo.javaas 中拥有相同的属性ThirdPartyPojo.java并从ThirdPartyPojo.javato填充数据OurOwnPojo.java并返回相同的数据?

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!

谢谢!

回答by Masudul

Probably you are searching Apache CommonsBeanUtils.copyProperties.

可能您正在搜索Apache CommonsBeanUtils.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;
}

回答by feuyeux

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

org.springframework.beans.BeanUtils 比 apache aone 好:

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

回答by Zon

Don't misjudge origin and destination pojos:

不要误判起点和终点 pojo:

try {

  BeanUtils.copyProperties(new DestinationPojo(), originPojo);

} catch (IllegalAccessException | InvocationTargetException e) {
  e.printStackTrace();
}