'Nested Unidirectional OnetoMany with Mapstruct Mapper interface returning null for the second level DTO class with
I have 4 classes with Unidirectional @OneToMany relationship as below:
Class A {
// some properties
List<B> bList = new ArrayList<>();
}
Class B has a Set of Class C
Class B {
// Some properties
Set<C> cSet = new HashSet<>();
}
Class C has a One to One relationship with Class D
Class C {
// Some properties here
D d;
}
AMapper interface looks below;
@Mapper(componentModel="spring", uses= {BMapper.class})
public interface AMapper {
@Mapping(target = "aId", source="id")
ADTO toDTO(final A a);
@Mapping(target = "id", ignore = true)
A toEntity(final ADTO aDTO);
}
BMapper as below:
@Mapper(componentModel="spring", uses= {CMapper.class})
public interface BMapper {
@Mapping(target = "bId", source="id")
@Mapping(target = "xxx", ignore = true)
@Mapping(target = "yyy", ignore = true)
BDTO toDTO(final BData entity);
@Mapping(target = "id", ignore = true)
B toEntity(final BDTO dto);
Iterable<B> bDtoToBListEntity(Iterable<BDTO> list);
Iterable<BDTO> bListEntityToBDto(Iterable<B> list);
}
CMapper as below
@Mapper(componentModel = "spring", uses = { DMapper.class })
public interface CMapper {
CDTO toDTO(C c);
@Mapping(target = "id", ignore = true)
C toEntity(CDTO planet);
Set<C> cDtoToCSetEntity(Set<CDTO> set);
Set<CDTO> cSetEntityToCDto(Set<C> set);
}
DMapper as below:
@Mapper(componentModel = "spring")
public interface DMapper {
DDTO toDTO(D d);
D toEntity(DDTO d);
}
@Service
AService {
public ADTO get(final Long id) {
return aMapper.toDTO( findById(id));
}
}
What is the problem? I am able to successfully read the bList when through the service class's get method however, cSet from class B is empty (not null though). Should I manually read the bDTO to populate the cSet? I hope not as I assume that's where the mapstruct is helping us. Any idea what is that I am missing? Thanks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
