-1

我有两个pojo。

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?

IE:

Pojo1 newPojo1 = SomeLibrary.fromPojoWithLikeNamedFields(pojo2);

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

1 回答 1

-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);

还是我误解了什么……?

于 2012-12-04T17:27:31.570 回答