I have the following mappers:
@Mapper(uses = { IItemMapper.class, IChainMapper.class, ICountryMapper.class, IForecastFeatureMapper.class })
public interface IForecastMapper {
ForecastModel fromForecastDTOToForecastModel(ForecastDTO dto);
}
@Mapper
public interface ICountryMapper {
@Mapping(target = "name", ignore = true)
@Mapping(target = "acronym", ignore = true)
@Mapping(target = "status", ignore = true)
CountryModel fromForecastDTOToCountryModel(ForecastDTO forecastDTO);
}
Given that the object ForecastModel is built like below:
@Entity
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "forecast")
public class ForecastModel implements Serializable {
@Serial private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@UuidGenerator
@com.enterprise.b2b.api.service.common.hibernate.constraint.UUID
private UUID id;
@NotNull
@ManyToOne
@JoinColumn(name = "country_id", referencedColumnName = "id", nullable = false)
private CountryModel countryId;
}
Isn’t mapStruct supposed to use the fromForecastDTOToCountryModel
method from ICountryMapper
inside fromForecastDTOToForecastModel
method from IForecastMapper
?
Instead of it mapStruct is generating a protected method inside the implementation like this:
protected CountryModel forecastDTOToCountryModel(ForecastDTO forecastDTO) {
if ( forecastDTO == null ) {
return null;
}
CountryModel.CountryModelBuilder countryModel = CountryModel.builder();
if ( forecastDTO.getCountryId() != null ) {
countryModel.id( UUID.fromString( forecastDTO.getCountryId() ) );
}
return countryModel.build();
}
Even when I try to force using @Mapping
annotation, mapStruct is still generating the method, for example:
@Mapper(uses = { IItemMapper.class, IChainMapper.class, ICountryMapper.class, IForecastFeatureMapper.class }, imports = Mappers.class)
public interface IForecastMapper {
@Mapping(target = "countryId", expression = "java(Mappers.getMapper(ICountryMapper.class).fromForecastDTOToCountryModel(dto))")
ForecastModel fromForecastDTOToForecastModel(ForecastDTO dto);
}
mapStruct simply ignores the expression and generates the method. I am using mapStruct 1.6.2.
Any thoughts?