开发者

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

Problem description:

I have a POJO object, which is mapped from database. Which having attributes(column) with same name as in database t开发者_如何学编程able. 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
}


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


My ModelMapper is another library worth checking out. It offers a fluent API to map properties as opposed to using string references or XML.

Check out the ModelMapper site for more info:

http://modelmapper.org


You can add @JsonProperty("salery") for mapping the EMP_SALERY field on the top of the field. which uses the import of import com.fasterxml.jackson.annotation.JsonProperty;

This works for me when using the ModelMapper.

If again deserialization is your concern you have 2 options.

@JsonProperty("EMP_SALERY")
    public byte getEMP_SALERY() {
    return red;
}

@JsonProperty("salery")
     public void setEMP_SALERY(byte red) {
     this.red = red;
}

The other way is using Alias

@JsonAlias({"salery", "EMP_SALERY"})
private String EMP_SALERY;


I suggest you try JMapper Framework. With a little configuration (with annotations or xml) you're ready to map

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜