简体   繁体   English

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

[英]Dto to Entity conversion using MapStruct gives error

I am trying to create a mapper of "PersonDto" class to "PersonEntity" using MapStruct 我正在尝试使用MapStruct将“ PersonDto”类的映射器创建为“ PersonEntity”

Please consider the following Entity, Dto and Mapper classes. 请考虑以下Entity,Dto和Mapper类。

PersonEntity.java 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 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 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 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 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);
}

When I am trying to generate code using the maven instructions provided at this link I am receiving the following error: 当我尝试使用链接提供的Maven指令生成代码时,出现以下错误:

Can't map property "java.util.List cars" to "com.example.car.CarEntity cars". 无法将属性“ java.util.List汽车”映射到“ com.example.car.CarEntity汽车”。 Consider to declare/implement a mapping method: "com.example.car.CarEntity map(java.util.List value)". 考虑声明/实现一个映射方法:“ com.example.car.CarEntity map(java.util.List value)”。

If i remove the "adder" method from Person classes it works fine. 如果我从Person类中删除“ adder”方法,它将很好地工作。 But I really want to use adder methods (for JPA entities parent child relationship purpose). 但是我真的很想使用加法器方法(用于JPA实体的父子关系)。

I am not sure why MapStruct not interpreting the cars property in PersonEntity as List. 我不确定MapStruct为什么不将PersonEntity中的cars属性解释为List。 It is interpreting that property as a simple CarEntity and not the List of CarEntity. 它将该属性解释为简单的CarEntity,而不是CarEntity列表。

You should consider mapping the "Has-A" property of your class too 您还应该考虑映射类的“ Has-A”属性
Just include CarEntity carDtoToCarEntity(CarDto carDto) 只需包含CarEntity carDtoToCarEntity(CarDto carDto)
Here is code snippet: 这是代码片段:

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

Let me know if you are still stuck. 让我知道你是否仍然被困住。

In your @Mapper 在您的@Mapper中

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

annotation you should use mapper for any kind of nested type you have in your entity 注释,您应该对实体中使用的任何嵌套类型使用mapper

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM