4

无法将源类到目标类的深度映射映射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());

    }
}
4

2 回答 2

3

在源类(SourceEmployee)中添加这个注解就足够了:

@Mapping("this")
private Address addr;

实际上,“this”的意思是“保持在同一个节点上”;之后,houseName 将在 Address 和 TargetEmployee 之间匹配。

;)

亚历克斯

于 2013-06-26T17:05:08.343 回答
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 中设置

于 2012-11-27T08:38:43.370 回答