I have a one to many relation between Config and ConfigHeaders. Here is the Config mapper:
@Mapper(componentModel = "spring", uses = {UserMapper.class, ConfigHeadersMapper.class})
public interface ConfigMapper extends EntityMapper<ConfigDTO, Config> {
@Mapping(source = "user.id", target = "userId")
ConfigDTO toDto(Config config);
@Mapping(source = "userId", target = "user")
@Mapping(target = "messages", ignore = true)
Config toEntity(ConfigDTO configDTO);
default Config fromId(Long id) {
if (id == null) {
return null;
}
Config config = new Config();
config.setId(id);
return config;
}
}
and here is the ConfigHeadersMapper:
@Mapper(componentModel = "spring", uses = {ConfigMapper.class})
public interface ConfigHeadersMapper extends EntityMapper<ConfigHeadersDTO, ConfigHeaders> {
@Mapping(source = "config.id", target = "configId")
ConfigHeadersDTO toDto(ConfigHeaders configHeaders);
@Mapping(source = "configId", target = "config")
ConfigHeaders toEntity(ConfigHeadersDTO configHeadersDTO);
default ConfigHeaders fromId(Long id) {
if (id == null) {
return null;
}
ConfigHeaders configHeaders = new ConfigHeaders();
configHeaders.setId(id);
return configHeaders;
}
}
When I try to save a new entity (ids equal to null for the Config & ConfigHeaders) this piece of code:
final Config config = configMapper.toEntity(configDTO);
Config newConfig = configRepository.save(config);
saves the Config and the ConfigHeaders but the config_id (FK) at the ConfigHeaders is NULL.
So I tried this code:
final Config config = configMapper.toEntity(configDTO);
config.getHeaders().stream().forEach(header -> header.setConfig(config));
Config newConfig = configRepository.save(config);
and indeed saves the children (ConfigHeaders) with the auto generated parent ID (config_id).
Could you tell me what am I doing wrong with the MapStruct? I am quite new with this tool and I do not believe this is the correct solution. I consider the previous code only a workaround for now.
Actually I have checked the MapStruct implementation code that have been generated and I have noted that it sets correctly the parent ID (that is null for new ones) but it does not set the backwards relationship with the father entity. How could I accomplish this through MapStruct?
Thank you in advance