将POJO内容从一个bean复制到另一个bean [英] Copy POJO content from one bean to another

查看:140
本文介绍了将POJO内容从一个bean复制到另一个bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在不同的包中有很少的Pojos,每个POJO包含来自同一个包的另一个pojo的集合。我需要将包裹B Pojos中具有相同名称的所有项目复制到包A中的对象。

I have few Pojos in different packages, each POJO contains set of the another pojo from the same package. I need to copy all items with the same name from Package B Pojos to objects in Package A.

Eaxmple:

package com.vanilla.packageA;

public class Student{

    private String firstName;
    private String lastName;
    private Set<Course> course;

    //getters and setters ommited

}   



< hr>


package com.vanilla.packageA;

    public class Course{
    private String courseName;
    private String courseDescription;

    //seters and getters
}







package com.vanilla.packageB;

public class Student{

    private String firstName;
    private String lastName;
    private Address address;
    private Set<Course> course;
    Private Date birtday;

    //getters and setters ommited

}   



< hr>


package com.vanilla.packageB;

public class Course{
    private String courseName;
    private String courseDescription;
    private <Lecturer> lecturer;
    private Integer hours;

    //seters and getters
} 

我想要将 PackageB 类中的所有项目递归地复制到 packageA 存在于 PaCkageB 并且共享相同的名称。

I want to copy recursively all items from PackageB classes to packageA classes which exists in PaCkageB and shares the same name.

更新:

伙计们,我明白这是愚蠢的问题,但我需要维护这段代码,现在代码的编写方式是他们必须调用50个getter和setter,或者用50个参数调用构造函数。
不幸的是,我不能使用相同的对象,我需要复制它,但我必须找到更多优雅的方式来复制tese bean。

Guys, I understand that that this is stupid question, but I need to maintain this code, now the code is written in the way that they have to call 50 getters and setter, or calling constructor with 50 parameters. Unfortunately, I can't use the same object and I need to copy it, but I must find more "elegant" way to copy tese beans.

推荐答案

Apache BeanUtils.copyProperties 不起作用?

Any reason why Apache BeanUtils.copyProperties does not work?

这篇关于将POJO内容从一个bean复制到另一个bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆