我在使用mapstruct映射器时遇到问题。运行mvn全新安装(或mvn全新编译)时,出现以下错误:
[ERROR] /mapper/EntityMapper.java:[28,7] Can't map property "java.util.List<com.socomec.tseselector.model.Source> architecture.sources
" to "java.lang.Integer architecture.sources". Consider to declare/implement a mapping method: "java.lang.Integer map(java.util.List<com.socomec.tseselector.model.Source> value)".
[ERROR] /mapper/command/TSEProjectCommandMapper.java:[21,16] Can't map property "java.lang.Integer architecture.loads" to "java.util.List<com.socomec.tseselector.model.Load> architecture.loads". Consider to declare/implement a mapping method: "java.util.List<com.socomec.tseselector.model.Load> map(java.lang.Integer value)".
问题是我不知道mapstruct是从哪里获得“ java.lang.Integer architecture.loads”的。 我不了解此Integer的来源,正如您在我的代码中看到的那样,没有Integer。同样直到现在,我在使用类似的映射器时也从未遇到过此错误。
这是我的实体架构:
public class Architecture {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String imagePath;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name="tse_architectures_loads",
joinColumns = {@JoinColumn(table = "tse_architecture", name = "architecture_id")},
inverseJoinColumns = {@JoinColumn(table = "tse_load", name = "load_id")}
)
private List<Load> loads;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name="tse_architectures_sources",
joinColumns = {@JoinColumn(table = "tse_architecture", name = "architecture_id")},
inverseJoinColumns = {@JoinColumn(table = "tse_source", name = "source_id")}
)
private List<Source> sources;
private String techno;
public Architecture() {
this.loads = new ArrayList<>();
this.sources = new ArrayList<>();
}
DTO
public class ArchitectureDto {
private Long id;
private String name;
private String imagePath;
private List<LoadDto> loadDtos;
private List<SourceDto> sourceDtos;
private String techno;
public ArchitectureDto() {
this.loadDtos = new ArrayList<>();
this.sourceDtos = new ArrayList<>();
}
}
我的映射器:
@Mapper(componentModel = "spring")
public interface ArchitectureMapper extends EntityMapper<ArchitectureDto, Architecture> {
@Mapping(source = "loads", target = "loadDtos")
@Mapping(source = "sources", target = "sourceDtos")
ArchitectureDto toDto(Architecture architecture);
}
实体映射器:
public interface EntityMapper<D, E> {
/**
* Map a DTO to an Entity
*
* @param dto the dto to map
* @return an Entity
*/
E toEntity(D dto);
/**
* Map an Entity to a DTO
*
* @param entity to map to a DTO
* @return a DTO
*/
D toDto(E entity);
/**
* Map a List of DTOs to a List of Entities
*
* @param dtoList the list to map
* @return a list of Entities
*/
List<E> toEntity(List<D> dtoList);
/**
* Map a list of Entities to a list of DTOs
*
* @param entityList the list to map
* @return a list of DTOs
*/
List<D> toDto(List<E> entityList);
}
在这里浏览有关mapstruct的文档和其他问题,到目前为止,我还没有找到解决方案。
我要添加负载和源类 加载:
public class Load {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String type;
private String unity;
private Long sourcePriority1;
private Long SourcePriority2;
private Long SourcePriority3;
private Long kvaValue;
private Long kwValue;
private Long pfValue;
private Long aValue;
private Long iccValue;
@JsonIgnore
@ManyToMany(mappedBy = "loads")
private List<TSEProject> tseProjects;
@JsonIgnore
@ManyToMany(mappedBy = "loadList")
private List<Architecture> architectures;
public Load() {
this.architectures = new ArrayList<>();
this.tseProjects = new ArrayList<>();
}
来源:
public class Source {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String type;
private String unity;
private Long quantity;
private Long kvaValue;
private Long kwValue;
private Long pfValue;
private Long aValue;
@JsonIgnore
@ManyToMany(mappedBy = "sources")
private List<TSEProject> tseProjects;
@JsonIgnore
@ManyToMany(mappedBy = "sourceList")
private List<Architecture> architectures;
public Source() {
this.architectures = new ArrayList<>();
this.tseProjects = new ArrayList<>();
}
}
和DTO
@Data
public class LoadDto {
private Long id;
private String name;
private String type;
private String unity;
private Long sourcePriority1;
private Long SourcePriority2;
private Long SourcePriority3;
private Long kvaValue;
private Long kwValue;
private Long pfValue;
private Long aValue;
private Long iccValue;
private List<Long> tseProjectsId;
public LoadDto() {
this.tseProjectsId = new ArrayList<>();
}
}
@Data
public class SourceDto {
private Long id;
private String name;
private String type;
private String unity;
private Long quantity;
private Long kvaValue;
private Long kwValue;
private Long pfValue;
private Long aValue;
}
答案 0 :(得分:0)
我想您还需要解决一些问题。尝试帮助MapStruct并定义这样的其他映射方法:
Mapper(componentModel = "spring")
public interface ArchitectureMapper extends EntityMapper<ArchitectureDto, Architecture> {
@Mapping(source = "loads", target = "loadDtos")
@Mapping(source = "sources", target = "sourceDtos")
ArchitectureDto toDto(Architecture architecture);
// first try with all properties ignored
// @Mapping( target = "id", ignore = true ) etc.
SourceDto toDto(Source source);
// first try with all properties ignored
// @Mapping( target = "id", ignore = true ) etc.
LoadDto toDto(Load load);
}
问题是MapStruct尝试根据属性名称自动映射这些对象。如果某处发现相同的名称,它将尝试进行类型转换或使用现有的映射方法来完成其工作。它甚至尝试2个步骤(先转换,再转换方法),有时会导致这些结果。
您可以提供MapStruct尝试自行生成的某些映射,然后再次忽略所有属性,如上例所示。一张一张地删除忽略行,看看何时MapStruct开始抱怨。
如果它再次是对象图,则可以应用类似的策略。
这样的调试有点乏味,但是可能在对象图的最深处发现了问题。然后,您可以再次删除所有签名,除了可以解决问题的签名以外,可以最大程度地利用代码生成。