我在春季启动应用程序中使用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映射错误:\ n \ n1)转换器
  org.modelmapper.internal.converter.NumberConverter@ad01ae2失败
  将entity.TaskEntity转换为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/

关于java - ModelMapper是否采用toString方法转换属性?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56681471/

10-12 01:54