Copy POJO content from one bean to another

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

}   

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
} 

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

Updates:

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.


Solution 1:

Any reason why Apache BeanUtils.copyProperties does not work?

Solution 2:

Well.. Dozer may be just the thing you're looking for.

. . . its an object to object mapping framework. The idea is that:

  • Usually it will map by convention.
  • You can override this convention with a mapping file.

. . therefore mapping files are as compact as possible. Its useful for many cases, such as mapping a use-case specify service payload on to the reusable core model objects.

When delivering the SpringSource training courses we used to point out this framework very often.

Solution 3:

See to mapstruct. This tools generates code, so there is no overhead on reflection.

Solution 4:

If you already have spring dependencies you could use org.springframework.beans.BeanUtils

BeanUtils.copyProperties(from, to);