带注释的推土机深度映射

发布于 2024-12-19 09:31:13 字数 1841 浏览 1 评论 0原文

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

    }
}

Unable to map deep mapping from Source class to Target class SourceEmployee.Address.houseName to TargetEmployee.houseName

Source object class

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;
    }   
}

Address class

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

target object

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;
    }
}

Main class copying from source to destination

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());

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水染的天色ゝ 2024-12-26 09:31:13

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

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;
    }
}

SourceEmployee 中的集合

@Mapping("empName")

不是必需的

In case someone else run into the same problem, I manage to correct that case by doing this :

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;
    }
}

The

@Mapping("empName")

set in SourceEmployee Isn't necessary

豆芽 2024-12-26 09:31:13

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

@Mapping("this")
private Address addr;

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

;)

亚历克斯

it's enough to add this annotation in the source class (SourceEmployee):

@Mapping("this")
private Address addr;

In fact, "this" means "remain on the same node"; after that, houseName will match between Address and TargetEmployee.

;)

Alex

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文