将字段从推土机中包含的父对象包含到嵌套集合对象中

时间:2019-07-04 10:04:03

标签: java dozer

我有Departments的列表,而列表又依次有Employee的列表,如下所示:

Employee emp1 = new Employee("100", "xyz");
Employee emp2 = new Employee("200", "pqr");

Department dept1 = new Department("10", "Account");

List<Employee> empList = new ArrayList<Employee>();

empList.add(emp1);
empList.add(emp2);

dept1.setEmpList(empList);

下面是DepartmentEmployee类:

public class Employee {
    private String id;
    private String name;
}

public class Department {
    private String id;
    private String name;
    List<Employee> empList;
}

我想要一个Employee列表,其中包含部门ID和名称

public class EmployeeD {
    private String id;
    private String name;
    private String deptId;
    private String deptName;
}

public class EmployeeDepartmentDTO {    
    private List<EmployeeD> empList;
}

我尝试过的操作如下:

<mapping map-id="depEmpD">
    <class-a>streammapper.Department</class-a>
    <class-b>streammapper.EmployeeD</class-b>
    <field>
        <a>id</a>
        <b>deptId</b>
    </field>
    <field>
        <a>name</a>
        <b>deptName</b>
    </field>
</mapping>
<mapping map-id="department">
    <class-a>streammapper.Department</class-a>
    <class-b>streammapper.EmployeeDepartmentDTO</class-b>

    <field map-id="depEmpD">
        <a>empList</a>
        <b>empList</b>
    </field>
</mapping>

通过以下映射,我可以在没有EmployeeDdeptId的情况下成功创建deptName列表:

<mapping map-id="depEmpD">
    <class-a>streammapper.Department</class-a>
    <class-b>streammapper.EmployeeD</class-b>
    <field>
        <a>id</a>
        <b>deptId</b>
    </field>
    <field>
        <a>name</a>
        <b>deptName</b>
    </field>
</mapping>
<mapping map-id="department">
    <class-a>streammapper.Department</class-a>
    <class-b>streammapper.EmployeeDepartmentDTO</class-b>
    <field>
        <a>empList</a>
        <b>empList</b>
    </field>
</mapping>

我需要一个EmployeeD列表,该列表还将在deptId对象中包含deptNameEmployeeDepartmentDTO

0 个答案:

没有答案