问题描述
我在不同的包中很少有 Pojo,每个 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.
示例:
package com.vanilla.packageA;
public class Student{
private String firstName;
private String lastName;
private Set<Course> course;
//getters and setters ommited
}
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
}
package com.vanilla.packageB;
public class Course{
private String courseName;
private String courseDescription;
private <Lecturer> lecturer;
private Integer hours;
//seters and getters
}
我想递归地将 PackageB
类中的所有项目复制到 PackageB
中存在并共享相同名称的 packageA
类.
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 beans.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!