How to map POJO in to DTO, if fields are with different name

Pankaj Kumar picture Pankaj Kumar · Jun 22, 2011 · Viewed 8.5k times · Source

Problem description:

I have a POJO object, which is mapped from database. Which having attributes(column) with same name as in database table. But it required some changes and I will have to use DTOs. But problem is that DTO having attributes with different names as defined in database table,(because of this I am mapping database table to POJO and then POJO to DTO) so at the time of mapping I have to use field and field mapping(One field of POJO and one field of DTO object), and that will take 50 lines of code( database table having 50 columns). Is there any solution to map directly to my POJO with DTO? Or is there a way to map database table to POJO/DTO if database table and POJO/DTO having diffrent column names?

For example

public class EmployeePOJO {

    String EMP_ID;
    String EMP_NAME;
    String EMP_SALERY;
    String EMP_DOB;
    String EMP_CONTACT_NO;
    String EMP_ADDRESS;
    String EMP_BLOOD_GROUP;
    String ASSIGNED_PROJECT;
    String PROJECT_MANAGER;
    String ROLE;

    //Getters and setters
}



public class EmployeeDTO {

    //String EMP_ID;
    //String EMP_NAME;
    String salery;            //EMP_SALERY;
    //String EMP_DOB;
    String phoneNumber;       //EMP_CONTACT_NO;
    String address;           //EMP_ADDRESS;
    //String EMP_BLOOD_GROUP;
    String currentProject;    //ASSIGNED_PROJECT;
    String projectManager;    //PROJECT_MANAGER;
    String role;              //ROLE;

    //getters and setters
}

Answer

Cris picture Cris · Jun 22, 2011

http://sourceforge.net/projects/dozer/develop ? (not sure it maps your needs,but take a look) http://dozer.sourceforge.net/