Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 617
danny.lesnik Avatar asked May 09 '11 13:05

danny.lesnik


2 Answers

Any reason why Apache BeanUtils.copyProperties does not work?

like image 52
Rosdi Kasim Avatar answered Oct 21 '22 13:10

Rosdi Kasim


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.

like image 39
flo Avatar answered Oct 21 '22 15:10

flo