gpt4 book ai didi

java - ModelMapper正在采用toString方法来转换属性?

转载 作者:行者123 更新时间:2023-12-01 19:37:55 28 4
gpt4 key购买 nike

我正在使用 ModelMapper 将实体映射到 Spring Boot 应用程序中的 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/

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com