Replies: 1 comment
-
MapStruct does not know about the expression you're providing. Therefore it does not know that it depends on Without seeing your models it's hard to provide the correct approach, but one of these might help: If @Mapper(nullValueMappingStrategy = org.mapstruct.NullValueMappingStrategy.RETURN_DEFAULT)
interface Issue3756Mapper {
@Mapping(target = "ownerBusinessUnit.ownerBusinessUnitId", source = "ownerBusinessUnitId")
KafkaTechResource mapCreationDtoToEntity(KafkaTechResourceCreationDto pCreationDto);
}
class BusinessUnit {
Long ownerBusinessUnitId;
public BusinessUnit(Long ownerBusinessUnitId) {
this.ownerBusinessUnitId = ownerBusinessUnitId;
}
} This will generate the following mapping code: @Override
public KafkaTechResource mapCreationDtoToEntity(KafkaTechResourceCreationDto pCreationDto) {
KafkaTechResource kafkaTechResource = new KafkaTechResource();
if ( pCreationDto != null ) {
kafkaTechResource.setOwnerBusinessUnit( kafkaTechResourceCreationDtoToBusinessUnit( pCreationDto ) );
kafkaTechResource.setName( pCreationDto.getName() );
kafkaTechResource.setDescription( pCreationDto.getDescription() );
}
return kafkaTechResource;
}
protected BusinessUnit kafkaTechResourceCreationDtoToBusinessUnit(KafkaTechResourceCreationDto kafkaTechResourceCreationDto) {
Long ownerBusinessUnitId = null;
if ( kafkaTechResourceCreationDto != null ) {
ownerBusinessUnitId = kafkaTechResourceCreationDto.getOwnerBusinessUnitId();
}
BusinessUnit businessUnit = new BusinessUnit( ownerBusinessUnitId );
return businessUnit;
} If this doesn't work for you, since Another option is to provide a custom mapping method that you can use: @Mapper(nullValueMappingStrategy = org.mapstruct.NullValueMappingStrategy.RETURN_DEFAULT)
interface Issue3756Mapper {
@Mapping(target = "ownerBusinessUnit", source = "ownerBusinessUnitId")
KafkaTechResource mapCreationDtoToEntity(KafkaTechResourceCreationDto pCreationDto);
default BusinessUnit buildBusinessUnit(Long ownerBusinessUnitId) {
return new BusinessUnit(ownerBusinessUnitId);
}
} which then generates: @Override
public KafkaTechResource mapCreationDtoToEntity(KafkaTechResourceCreationDto pCreationDto) {
KafkaTechResource kafkaTechResource = new KafkaTechResource();
if ( pCreationDto != null ) {
kafkaTechResource.setOwnerBusinessUnit( buildBusinessUnit( pCreationDto.getOwnerBusinessUnitId() ) );
kafkaTechResource.setName( pCreationDto.getName() );
kafkaTechResource.setDescription( pCreationDto.getDescription() );
}
return kafkaTechResource;
} I hope this helps in your case. If not, please provide more details about your use case and your models. |
Beta Was this translation helpful? Give feedback.
-
I have the following mapping:
Note that the expression was added out of the
if ( pCreationDto != null )
block:I would like to also put the expression inside the block, is there a way to set that ?
thanks
Beta Was this translation helpful? Give feedback.
All reactions