【发布时间】:2019-07-24 19:16:00
【问题描述】:
我有以下实体类和类似的 DTO 类:
class Car {
Long id;
List<Owner> ownerList;
}
class Owner {
Long id;
String name;
}
我使用 MapStruct 与以下映射:
- 复制到没有 ID 的 CarDto
@Mapping(target = "id", ignore = true)
@Mapping(target = "ownerList", qualifiedByName = "withoutIdDto")
CarDto carToCarDto(Car car);
@Named("withoutIdDto")
@Mapping(target = "id", ignore = true)
OwnerDto mapOwnerDtoWithoutId(Owner owner);
- 无 ID 克隆
@Mapping(target = "id", ignore = true) //ignore Car.id
@Mapping(target = "ownerList", qualifiedByName = "withoutId")
Car copyCar(Car car);
@Named("withoutId")
@Mapping(target = "id", ignore = true) //ignore Owner.id
Owner mapOwnerWithoutId(Owner owner);
问题是:
为 carToCarDto() 生成的映射器正在调用 mapOwnerDtoWithoutId(),但 copyCar 方法没有调用 mapOwnerWithoutId()。这是生成方法的 sn-p:
public Car copyCar(Car car) {
if (car == null) {
return null;
} else {
Car car1 = new Car();
List<Owner> list = car.getOwnerList();
if (list != null) {
car1.setOwnerList(new ArrayList(list)); // no reference to mapOwnerWithoutId
}
return car1;
}
}
public CarDto carToCarDto(Car car) {
if (car == null) {
return null;
} else {
CarDto carDto = new CarDto();
carDto.setOwnerList(this.ownerListToOwnerDtoList(car.getOwnerList())); //ownerListToOwnerDtoList () calls mapOwnerDtoWithoutId
return carDto;
}
}
我已经关注项目来重现这一点。知道如何修复测试 CarMapperTest 吗?
https://github.com/gtiwari333/mapstruct-failing-test-same-object-copy
【问题讨论】: