java - 如何在 Java 中将属性映射到 Integer Mapstruct

标签 java spring mapstruct

我在使用映射结构映射器时遇到问题。运行 mvn clean install (或 mvn cleancompile)时,出现以下错误:

[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”。 我不明白这个整数是从哪里来的,正如你在我的代码中看到的那样,没有整数。而且到目前为止,我在使用类似的映射器时从未遇到过此错误。

这是我的实体架构:

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;

}

最佳答案

我想您需要进行更多故障排除..尝试帮助 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 何时开始提示。

如果它又是一个对象图,您可以应用类似的策略。

这样的调试有点乏味,但您可能会在对象图的深处发现问题。然后,您可以再次删除所有签名(除了解决您的问题的签名之外),从而最大限度地利用代码生成。

关于java - 如何在 Java 中将属性映射到 Integer Mapstruct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58431155/

相关文章:

java - 如何使用java检查运行时我的应用程序中的图像(16位,灰度)是否有任何变化?

java - Quartz Scheduler Job 运行一次,然后报错

java - Redis:在关闭其中一个 Redis 主节点时,Spring Boot 应用程序请求一直失败

java - 映射器没有使用另一个映射器,如何使用另一个映射器的映射器?

java - MapStruct - 如何忽略 POJO 中不必要的方法/非 getter-setter 方法

java - 文件目标选择器

java - 强制 Spring RestTemplate 使用 XmlConverter

spring - UI.getCurrent 在 Spring Managed Bean 中返回 Null

java - REST 客户端 restTemplate 无法获取对象集合

java - Mapstruct:将多个源对象映射到子对象