You can achieve that through @AfterMapping
with @MappedTarget
. This is described in the reference documentation: 12.2. Mapping customization with before-mapping and after-mapping methods.
// Java 8+ otherwise you need to use an abstract class and a for-loop instead
@Mapper(componentModel = "spring")
public interface ScheduledJobMapper {
@Mapping(target = "parameters", source = "paramtersDTOs")
ScheduledJob mapToDomain(ScheduledJobDTO dto);
@AfterMapping
default void after(@MappingTarget ScheduledJob domain, ScheduledJobDTO dto) {
domain.getParameters().forEach(scheduledJobParams -> {
scheduledJobParams.setScheduledJob(domain);
});
}
}
However, I am sure you don’t need to fill the bidirectional relationship when you map back from the DTO into the entity (this is what I understand as you refer to “domain”). Note printing out or serializing such object i.e. into JSON or XML throws java.lang.StackOverflowError
if not properly handled.
CLICK HERE to find out more related problems solutions.