使用MapStruct进行Dto到实体的转换会产生错误

时间:2018-08-18 17:36:30

标签: java spring-boot entity dto mapstruct

我正在尝试使用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汽车”映射到   “ com.example.car.CarEntity汽车”。考虑声明/实施   映射方法:“ com.example.car.CarEntity   map(java.util.List value)“。

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

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

2 个答案:

答案 0 :(得分:0)

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

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

}

@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED) public interface PersonMapper { PersonMapper INSTANCE = Mappers.getMapper(PersonMapper.class); CarEntity carDtoToCarEntity(CarDto carDto); PersonEntity personDtoToPersonEntity(PersonDto personDto); } 让我知道你是否仍然被困住。

答案 1 :(得分:0)

在您的@Mapper中

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

注释,对于实体中具有的任何嵌套类型,都应使用mapper