2

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

我的实体具有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 个的问题,但我需要做什么才能使它起作用。

4

0 回答 0