使用 MapStruct 将 Dto 转换为实体会出错
Dto to Entity conversion using MapStruct gives error
我正在尝试使用 MapStruct
创建 "PersonDto" class 到 "PersonEntity" 的映射器
请考虑以下 Entity、Dto 和 Mapper classes.
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 link 提供的 Maven 指令生成代码时,我收到以下错误:
Can't map property "java.util.List cars" to
"com.example.car.CarEntity cars". Consider to declare/implement a
mapping method: "com.example.car.CarEntity
map(java.util.List value)".
如果我从 Person classes 中删除 "adder" 方法,它就可以正常工作。但我真的很想使用加法器方法(用于 JPA 实体父子关系目的)。
我不确定为什么 MapStruct 不将 PersonEntity 中的汽车 属性 解释为列表。它将 属性 解释为一个简单的 CarEntity 而不是 CarEntity 列表。
您也应该考虑映射 class 的 "Has-A" 属性
只需包含 CarEntity carDtoToCarEntity(CarDto carDto)
这是代码片段:
</p>
<pre><code>@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED)
public interface PersonMapper {
PersonMapper INSTANCE = Mappers.getMapper(PersonMapper.class);
CarEntity carDtoToCarEntity(CarDto carDto);
PersonEntity personDtoToPersonEntity(PersonDto personDto);
}
如果您仍然卡住,请告诉我。
在你的@Mapper
@Mapper(componentModel = "spring",uses = CarMapper.class)
您应该为实体中的任何类型的嵌套类型使用映射器的注释
我正在尝试使用 MapStruct
创建 "PersonDto" class 到 "PersonEntity" 的映射器请考虑以下 Entity、Dto 和 Mapper classes.
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 link 提供的 Maven 指令生成代码时,我收到以下错误:
Can't map property "java.util.List cars" to "com.example.car.CarEntity cars". Consider to declare/implement a mapping method: "com.example.car.CarEntity map(java.util.List value)".
如果我从 Person classes 中删除 "adder" 方法,它就可以正常工作。但我真的很想使用加法器方法(用于 JPA 实体父子关系目的)。
我不确定为什么 MapStruct 不将 PersonEntity 中的汽车 属性 解释为列表。它将 属性 解释为一个简单的 CarEntity 而不是 CarEntity 列表。
您也应该考虑映射 class 的 "Has-A" 属性
只需包含 CarEntity carDtoToCarEntity(CarDto carDto)
这是代码片段:
</p>
<pre><code>@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED)
public interface PersonMapper {
PersonMapper INSTANCE = Mappers.getMapper(PersonMapper.class);
CarEntity carDtoToCarEntity(CarDto carDto);
PersonEntity personDtoToPersonEntity(PersonDto personDto);
}
如果您仍然卡住,请告诉我。
在你的@Mapper
@Mapper(componentModel = "spring",uses = CarMapper.class)
您应该为实体中的任何类型的嵌套类型使用映射器的注释