Java 使用MapStruct将Dto转换为实体时出错
我正在尝试使用Java 使用MapStruct将Dto转换为实体时出错,java,spring-boot,entity,dto,mapstruct,Java,Spring Boot,Entity,Dto,Mapstruct,我正在尝试使用 请考虑以下实体,Dto和映射器类。 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() {
请考虑以下实体,Dto和映射器类。
PersonEntity.javapackage 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;
}
}
package com.example.car;
public class CarEntity {
private String model;
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
}
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;
}
}
PersonDto.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;
}
}
package com.example.car;
public class CarEntity {
private String model;
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
}
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;
}
}
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);
}
当我试图使用link中提供的maven指令生成代码时,我收到以下错误:
无法将属性“java.util.List cars”映射到
“com.example.car.CarEntity cars”。考虑宣布/实施
映射方法:“com.example.car.CarEntity
映射(java.util.List值)”
如果我从Person类中删除“adder”方法,它就可以正常工作。但我确实想使用加法器方法(用于JPA实体父子关系)
我不知道为什么MapStruct不将PersonEntity中的cars属性解释为List。它将属性解释为一个简单的实体而不是实体的列表。 < P>你应该考虑映射你的类的“HAS-A”属性
只需将CarEntity CardToCardTo实体(CarDto CarDto)包括在内即可。
下面是代码片段:
如果您仍然卡在@Mapper中,请告诉我。
@Mapper(componentModel = "spring",uses = CarMapper.class)
注释对于实体中的任何类型的嵌套类型,都应该使用mapper,谢谢。它工作得很好。但有一件事我不明白,1.2.0版本的MapStruct支持“自动创建嵌套映射方法”,如链接中所述。目前我使用的是1.3.0.Beta1。那么为什么我需要为嵌套属性“cars”定义一个映射呢?这在1.2.0中有效吗?是,不需要创建
cardtocarentity
方法。也许这是一个错误。你能用你的问题开一张票让我们看看吗?注意:我是MapStruct的成员之一team@Filip我认为匹配问题已经产生。我在那里加了我的评论。这是你的电话号码。