如何使用MapStruct映射嵌套集合?

时间:2017-08-22 09:43:00

标签: java collections mapstruct

我有两个实体:

实体1:

public class Master {

    private int id;
    private Set<SubMaster> subMasters= new HashSet<SubMaster>(0);
}

public class SubMaster{
    private int subId;
    private String subName;
}

实体2:

public class MasterDTO {

    private int id;
    private Set<SubMaster> subMasters= new HashSet<SubMaster>(0);
}

public class SubMasterDTO{
    private int subId;
    private String subName;
}

我正在使用MapStruct Mapper将POJO的值映射到另一个。

public interface MasterMapper{
    MasterDTO toDto(Master entity);
}

我能够成功将Master映射到MasterDTO。但是,SubMasterMaster的嵌套集合未映射到MasterDTO中的对应集合。

有人能帮助我朝正确的方向发展吗?

1 个答案:

答案 0 :(得分:6)

Mapstruct的Github回购中的

This example可以准确展示您正在尝试做的事情。

TL; DR您需要为Select DateName(Month, DateSubmitted) As 'Month', Sum(Case When OverallRiskRating = 1 Then 1 End) As 'Low', Sum(Case When OverallRiskRating = 2 Then 1 End) As 'Med', Sum(Case When OverallRiskRating = 3 Then 1 End) As 'High' From dbo.ChangeEvaluationForm Where DateSubmitted >= DateAdd(Month, DateDiff(Month, 0, DateAdd(Month, -6, GetDate())), 0) Group By DateName(Month, DateSubmitted) (让我们称之为SubMaster)课程单独设置一个映射器,然后在SubMasterMapper注释上添加@Mapper(uses = { SubMasterMapper.class })注释{1}}。