Orika映射不起作用

时间:2015-07-29 02:53:18

标签: java orika

我已经为对象HomeContentDTO和SubscriberUpsertDTO

定义了我的映射
public class HomeContentDTO implements Serializable {

private static final long serialVersionUID = 1717544153530572470L;

private String fileName;
private String subscriberName;
private String subscriberEmail;
private String subscriberCompanyName;

public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

public String getSubscriberName() {
    return subscriberName;
}

public void setSubscriberName(String subscriberName) {
    this.subscriberName = subscriberName;
}

public String getSubscriberEmail() {
    return subscriberEmail;
}

public void setSubscriberEmail(String subscriberEmail) {
    this.subscriberEmail = subscriberEmail;
}

public String getSubscriberCompanyName() {
    return subscriberCompanyName;
}

public void setSubscriberCompanyName(String subscriberCompanyName) {
    this.subscriberCompanyName = subscriberCompanyName;
}
}


public class SubscriberUpsertDTO implements Serializable{

    private static final long serialVersionUID = -6899387128123861532L;
    private Integer id;
    private String name;
    private String email;
    private String companyName;

    public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getCompanyName() {
    return companyName;
}

public void setCompanyName(String companyName) {
    this.companyName = companyName;
}

public boolean isActive() {
    return active;
}

public void setActive(boolean active) {
    this.active = active;
}
}

以下是两个对象的映射配置

public class HomeMapperRegister {
    @MapperRegister
    public void register(MapperFactory mapperFactory) {
         mapperFactory.classMap(HomeContentDTO.class, SubscriberUpsertDTO.class)
            .fieldAToB("subscriberName", "name")
            .fieldAToB("subscriberEmail", "email")
            .fieldAToB("subscriberCompanyName", "companyName")
            .register();
    }
}

HomeContentDTO中的所有映射值都不会复制到SubscriberUpsertDTO。有谁知道原因是什么?

SubscriberUpsertDTO subscriberUpsertDTO =     mapperFactory.getMapper().map(homeContentDTO, SubscriberUpsertDTO.class);

1 个答案:

答案 0 :(得分:2)

class HomeMapperRegister extends ConfigurableMapper {

    @Override
    public void configure(MapperFactory mapperFactory) {
        mapperFactory.classMap(HomeContentDTO.class, SubscriberUpsertDTO.class)
                .fieldAToB("subscriberName", "name")
                .fieldAToB("subscriberEmail", "email")
                .fieldAToB("subscriberCompanyName", "companyName")
                .register();
    }
}
来自extendsConfigurableMapper Override

configure来映射。