【问题标题】:Dto to Entity conversion using MapStruct gives error使用 MapStruct 将 Dto 转换为实体会出错
【发布时间】:2018-08-18 17:36:30
【问题描述】:

我正在尝试使用 MapStruct 将“PersonDto”类创建为“PersonEntity”的映射器

请考虑以下 Entity、Dto 和 Mapper 类。

PersonEntity.java

package com.example.car;

import java.util.ArrayList;
import java.util.List;

public class PersonEntity {
    private String name;
    private List<CarEntity> cars;

    public PersonEntity() {
        cars = new ArrayList<>();
    }

    public boolean addCar(CarEntity carEntitiy) {
        return cars.add(carEntitiy);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<CarEntity> getCars() {
        return cars;
    }

    public void setCars(List<CarEntity> carEntities) {
        this.cars = carEntities;
    }
}

CarEntity.java

package com.example.car;

public class CarEntity {
    private String model;

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }
}

PersonDto.java

package com.example.car;

import java.util.ArrayList;
import java.util.List;

public class PersonDto {
    private String name;
    private List<CarDto> cars;

    public PersonDto() {
        cars = new ArrayList<>();
    }

    public boolean addCar(CarDto carDto) {
        return cars.add(carDto);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<CarDto> getCars() {
        return cars;
    }

    public void setCars(List<CarDto> carDtos) {
        this.cars = carDtos;
    }
}

CarDto.java

package com.example.car;

public class CarDto {
    private String model;

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }
}

PersonMapper

package com.example.car;

import org.mapstruct.CollectionMappingStrategy;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED)
public interface PersonMapper {

    PersonMapper INSTANCE = Mappers.getMapper(PersonMapper.class);

    PersonEntity personDtoToPersonEntity(PersonDto personDto);
}

当我尝试使用 this 链接提供的 maven 指令生成代码时,我收到以下错误:

无法将属性“java.util.List cars”映射到 “com.example.car.CarEntity 汽车”。考虑声明/实施 映射方法:“com.example.car.CarEntity 地图(java.util.List 值)”。

如果我从 Person 类中删除“加法器”方法,它可以正常工作。但我真的很想使用加法器方法(用于 JPA 实体的父子关系目的)。

我不确定为什么 MapStruct 不将 PersonEntity 中的 cars 属性解释为 List。它将该属性解释为一个简单的 CarEntity 而不是 CarEntity 的列表。

【问题讨论】:

    标签: java spring-boot entity dto mapstruct


    【解决方案1】:

    您也应该考虑映射类的“Has-A”属性
    只需包含 CarEntity carDtoToCarEntity(CarDto carDto)
    这是代码sn-p:

    @Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED)
    public interface PersonMapper {
        PersonMapper INSTANCE = Mappers.getMapper(PersonMapper.class);
        CarEntity carDtoToCarEntity(CarDto carDto); 
        PersonEntity personDtoToPersonEntity(PersonDto personDto);
    
    }
    

    如果您仍然卡住,请告诉我。

    【讨论】:

    • 谢谢。它运作良好。但我不明白的一件事是,从 1.2.0 MapStruct 开始支持“自动创建嵌套映射方法”,如this 链接中所述。目前我使用的是 1.3.0.Beta1。那么为什么我需要为嵌套属性“cars”定义一个映射呢?
    • 这适用于 1.2.0 吗?是的,应该不需要创建 carDtoToCarEntity 方法。也许这是一个错误。能否请您为您的问题开一张票,以便我们查看?注意:我是 MapStruct 团队的成员之一
    • @Filip 我认为匹配问题已经创建。我在那里添加了我的评论。这是link
    【解决方案2】:

    在你的@Mapper 中

    @Mapper(componentModel = "spring",uses = CarMapper.class) 
    

    您应该为实体中的任何嵌套类型使用映射器的注释

    【讨论】:

      猜你喜欢
      • 2019-04-22
      • 1970-01-01
      • 2017-09-17
      • 2020-09-04
      • 1970-01-01
      • 2016-06-10
      • 1970-01-01
      • 2019-05-01
      • 2021-12-15
      相关资源
      最近更新 更多