Reputation: 73
I am trying to create a mapper of "PersonDto" class to "PersonEntity" using MapStruct
Please consider the following Entity, Dto and 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);
}
When I am trying to generate code using the maven instructions provided at this link I am receiving the following error:
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)".
If i remove the "adder" method from Person classes it works fine. But I really want to use adder methods (for JPA entities parent child relationship purpose).
I am not sure why MapStruct not interpreting the cars property in PersonEntity as List. It is interpreting that property as a simple CarEntity and not the List of CarEntity.
Upvotes: 0
Views: 5219
Reputation: 1645
In your @Mapper
@Mapper(componentModel = "spring",uses = CarMapper.class)
annotation you should use mapper for any kind of nested type you have in your entity
Upvotes: 0
Reputation: 741
You should consider mapping the "Has-A" property of your class too
Just include 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.
Upvotes: 1