ModelMapper 是用toString 方法转换属性?

ModelMapper is taking toString method to convert property?

我在 spring 引导应用程序中使用 ModelMapper 将实体映射到 DTO。

现在我有一些奇怪的东西我有这个实体:

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MileStoneEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    @ManyToOne(targetEntity = ProjectEntity.class, fetch = FetchType.LAZY)
    @JoinColumn(name = "projectEntity_id")
    private ProjectEntity project;
    @ManyToMany(targetEntity = CompanyUserEntity.class)
    private Set<CompanyUserEntity> projectManagers;
    @OneToMany(targetEntity = TaskEntity.class, mappedBy = "mileStone")
    private Set<TaskEntity> tasks;
    private boolean archived;

        public List<Long> projectManagersIds() {
        return projectManagers.stream().map(CompanyUserEntity::getId).collect(Collectors.toList());
    }

    public List<Long> taskIds() {
        return tasks.stream().map(TaskEntity::getId).collect(Collectors.toList());
    }
}

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TaskEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    @ManyToOne(targetEntity = ProjectEntity.class, fetch = FetchType.LAZY)
    @JoinColumn(name = "projectEntity_id")
    @ManyToOne(targetEntity = CompanyUserEntity.class, fetch = FetchType.LAZY)
    private CompanyUserEntity executor;
    @ManyToOne(targetEntity = MileStoneEntity.class, fetch = FetchType.LAZY)
    @JoinColumn(name = "mileStoneEntity_id")
    private MileStoneEntity mileStone;
    private boolean archived;
    private boolean replaceStartAndEndDateWithMileStone;
}

public static PropertyMap<MileStoneEntity, MileStoneDto> mileStoneEntityToMap = new PropertyMap<MileStoneEntity, MileStoneDto>() {
    protected void configure() {
        map().setProjectId(source.getProject().getId());
        map().setProjectManagers(source.projectManagersIds());
        map().setTasks(source.taskIds());
    }
};

DTO

@Data
@AllArgsConstructor
@NoArgsConstructor
public class MileStoneDto {
    private Long id;
    private String name;
    private Long projectId;
    private List<Long> projectManagers;
    private LocalDate startDate;
    private LocalDate endDate;
    private String description;
    private List<Long> tasks;
    private boolean archived;
}

现在这不起作用我得到这个错误:

"ModelMapper mapping errors:\n\n1) Converter org.modelmapper.internal.converter.NumberConverter@ad01ae2 failed to convert entity.TaskEntity to java.lang.Long

但是当我在 TaskEntity 中添加这样的 toString 方法时:

@Override
public String toString() {
    return ""+id+"";
}

然后一切正常。那么,为什么 ModelMapper 使用 toString 将 id 转换为 Long?

编辑

@Bean
ModelMapper modelMapper() {
    ModelMapper modelMapper = new ModelMapper();

    modelMapper.addMappings(ProjectMappings.mileStoneEntityToMap);


    return modelMapper;
}

您的实体和 dto 中的类型不同:

实体

private Set<TaskEntity> tasks;

DTO

private List<Long> tasks;

ModelMapper 不知道如何将 TaskEntity 转换为 Long。

你必须为此编写一个转换器:

http://modelmapper.org/user-manual/converters/