ModelMapper跳过不使用Map<>的空字段

时间:2018-06-02 17:42:14

标签: java modelmapper

我试图将(来源) RoadAssistanceDto映射到(目的地) RoadAssistance。两者都有Map<Locale, RoadAssistanceI18n>属性和 id proeprty。

public class RoadAssistanceDto{

 private Long id;
 private Map<Locale, RoadAssistanceI18n> roadAssistanceI18n

 // GETTERS AND SETTRS ///
}

public class RoadAssistance{

 private Long id;
 private Map<Locale, RoadAssistanceI18n> roadAssistanceI18n

 // GETTERS AND SETTRS ///
}


public class RoadAssistanceI18n{

private Long id;
private String description;

// GETTERS and settres/

}

问题我想保留RoadAssistanceI18n中的RoadAssistance ID

简单的测试用例:

@Test
void testMapper(){

    RoadAssistanceDto roadAssistanceDto = new RoadAssistanceDto();
    RoadAssistanceI18n roadAssistanceDtoI18n = new RoadAssistanceI18n();
    roadAssistanceDtoI18n.setDescription("NEW DESCRIPTION");
    roadAssistanceDto.setRoadAssistanceI18n(Locale.ENGLISH,roadAssistanceDtoI18n);

    RoadAssistance roadAssistance = new RoadAssistance();
    RoadAssistanceI18n roadAssistanceI18n = new RoadAssistanceI18n();
    roadAssistanceI18n.setId(10L);
    roadAssistanceI18n.setDescription("OLD DESCRIPTION");
    roadAssistance.setRoadAssistanceI18n(Locale.ENGLISH,roadAssistanceI18n);

    ModelMapper mapper = new ModelMapper();
    mapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());

    mapper.map(roadAssistanceDto,roadAssistance);

    System.out.println(roadAssistance.getRoadAssistanceI18n(Locale.ENGLISH).getId());
    System.out.println(roadAssistance.getRoadAssistanceI18n(Locale.ENGLISH).getDescription());

}

,结果是

  

     

新描述

从源(RoadAssistanceDto)覆盖描述字段是完美的,但 id 设置为 null 。我想将id保存为10(设置为RoadAssistance)

0 个答案:

没有答案