ModelMapper将一个对象映射为三个

时间:2016-03-03 11:59:39

标签: java modelmapper

在我的Java项目中,我使用ModelMapper将entity对象映射到DTO对象。

我的实体内部对象hierarchy有三个Long属性。 我的DTO对象只有一个Long属性,因此我创建了自定义地图以在它们之间进行映射。

public class MyMap extends PropertyMap<DTO, Entity> {

  /**
   * Main function that is called when mapping objects.
   */
  @Override
  protected void configure() {

    final Converter<Long, Hierarchy> hierarchyToId = context -> {

      if (context.getSource() != null) {

        final Long Id = context.getSource();

        final HierarchyDto dto = calculateHierarchy(Id)

        // map dto to entity.
        final Hierarchy hierarchy = new Hierarchy();
        hierarchy.setFirstId(dto.getFirstId());
        hierarchy.setSecondId(dto.getSecondId());
        hierarchy.setThirdId(dto.getThirdId());

        return hierarchy;
      }

      return null;
     };

    // map objects.
    this.using(hierarchyToId).map(this.source.getId(), this.destination.getHierarchy());
  }

}

这给我一个错误:

java.lang.NullPointerException: null ...

ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.modelmapper.ConfigurationException: ModelMapper configuration errors:
1) The destination property com....DTO.setId() matches multiple source property hierarchies:

com....getHierarchy()/com....getFirstId()
com....getHierarchy()/com....getSecondId()
com....getHierarchy()/com....getThirdId()

1 error] with root cause
org.modelmapper.ConfigurationException: ModelMapper configuration errors:

我理解Mapper有一些问题我将一个对象映射到3但我需要做什么才能使用。

0 个答案:

没有答案