将具有相同名称字段的两个pojos同步

时间:2012-12-04 17:21:06

标签: java automatic-properties

我有两个pojos。

public class Pojo1 implements Serializable {

    private static final long serialVersionUID = 1302290920579795856L;

    private Long id;
    private String idNumber; 
    private String vendorNumber; 
    private Date expires;

    // Getters and setters for each one
}

public class Pojo2 implements Serializable {

    private static final long serialVersionUID = 1302290920579795856L;

    private Long id;
    private String idNumber; 
    private String vendorNumber; 
    private Date expires;
    private String otherData;

    // Getters and setters for each one
}

我是否可以使用Java API自动从Pojo2创建Pojo1?

即:

Pojo1 newPojo1 = SomeLibrary.fromPojoWithLikeNamedFields(pojo2);

// newPojo1 now has all the fields that had the same name from pojo2

1 个答案:

答案 0 :(得分:-1)

复制构造函数不会完成这项工作吗?

public Pojo1(Pojo2 pojo2){
    this.id = pojo2.getId();
    this.idNumber = pojo2.getIdNumber();
    this.vendorNumber = pojo2.getVendorNumber();
    this.expires = pojo2.getExpires();
}

然后使用,

Pojo1 newPojo1 = new Pojo1(pojo2);

或者我误解了什么......?