带注释的推土机深度映射

时间:2011-12-02 18:05:00

标签: java dozer

无法将源类的深度映射映射到目标类SourceEmployee.Address.houseNameTargetEmployee.houseName

源对象类

public class SourceEmployee 
{   
    private String empName;
    private Address addr;


    enter code here

    @Mapping("empName")
    public String getEmpName() {
        return empName;
    }
    public void setEmpName(String empName) {
        this.empName = empName;
    }   
    public Address getAddr() {
        return addr;
    }

    public void setAddr(Address addr) {
        this.addr = addr;
    }   
}

地址类

public class Address 
{
    private String houseName;
    public String getHouseName() {
        return houseName;
    }
    public void setHouseName(String houseName) {
        this.houseName = houseName;
    }
}

目标对象

public class TargetEmployee 
{

    private String empName;
    private String houseName;

    public String getHouseName() {
        return houseName;
    }
    public void setHouseName(String houseName) {
        this.houseName = houseName;
    }
    public String getEmpName() {
        return empName;
    }
    public void setEmpName(String empName) {
        this.empName = empName;
    }
}

从源到目的地的主类复制

public class CopyAttributes 
{

    public static void main(String args[])
    {
        SourceEmployee sourceEmp = new SourceEmployee();
        sourceEmp.setEmpName("Rafi");

        Address adr = new Address();        
        adr.setHouseName("Aashiyana");
        sourceEmp.setAddr(adr);

        Mapper mapper = new DozerBeanMapper();
        TargetEmployee destObject = mapper.map(sourceEmp, TargetEmployee.class);

        System.out.println(destObject.getEmpName());
        System.out.println(destObject.getHouseName());

    }
}

2 个答案:

答案 0 :(得分:3)

如果其他人遇到同样的问题,我设法通过这样做来纠正这种情况:

public class TargetEmployee 
{

    private String empName;
    private String houseName;

    @Mapping("addr.houseName")
    public String getHouseName() {
        return houseName;
    }
    public void setHouseName(String houseName) {
        this.houseName = houseName;
    }
    public String getEmpName() {
        return empName;
    }
    public void setEmpName(String empName) {
        this.empName = empName;
    }
}

@Mapping("empName")

在SourceEmployee中设置没有必要

答案 1 :(得分:2)

足以在源类(SourceEmployee)中添加此注释:

@Mapping("this")
private Address addr;

事实上,“这个”意味着“保持在同一个节点上”;之后,houseName将匹配Address和TargetEmployee。

亚历