Reputation: 24659
I am facing an issue with one of my Mapstruct mappers not using another mapper with @Mapper(uses =
Our ValidationSupportNeedMapper
maps from entities to DTOs. One ValidationSupportNeedEntity
contains an ActivityEntity
property and I am trying to map from this property to an Activity
DTO.
The issue is therefore with a nested object i.e. ActivityEntity
to Activity
.
Here is the source code:
From ValidationSupportNeedMapper.java:
@Mapper(uses = {LifecycleErrorMessagesMapper.class, ActivityMapper.class})
public interface ValidationSupportNeedMapper {
ValidationSupportNeed toValidationSupportNeed(ValidationSupportNeedEntity source);
...
From ActivityMapper.java:
@Component
public class ActivityMapper {
public Activity toActivity(ActivityEntity activity) {
//Implementation
}
public ActivityEntity toActivityEntity(Activity activity) {
//Implementation
}
}
From ValidationSupportNeedEntity.java (Entity)
public class ValidationSupportNeedEntity {
private ActivityEntity activityEntity;
From ValidationSupportNeed.java (DTO)
public class ValidationSupportNeed implements AutoValidated {
private Activity validationActivity;
However Mapstruct seems to ignore the uses=
attribute on the @Mapper
annotation and goes ahead and generates its own mapping method as follows:
@Override
public ValidationSupportNeed toValidationSupportNeed(ValidationSupportNeedEntity source) {
if ( source == null ) {
return null;
}
ValidationSupportNeed validationSupportNeed = new ValidationSupportNeed();
validationSupportNeed.setValidationActivity( validationSupportNeedEntityToActivity( source ) );
...
}
protected Activity validationSupportNeedEntityToActivity(ValidationSupportNeedEntity validationSupportNeedEntity) {
if ( validationSupportNeedEntity == null ) {
return null;
}
Activity activity = new Activity();
activity.setCode( validationSupportNeedEntity.getValidationActivityCode() );
return activity;
}
What am I missing? Can someone please help?
edit: ActivityMapper
is not autowired into the ValidationSupportNeedMapper
implementation.
Upvotes: 3
Views: 16358
Reputation: 24659
Adding a mapping annotation sorted the issue:
@Mapping(source = "activityEntity", target = "validationActivity")
ValidationSupportNeed toValidationSupportNeed(ValidationSupportNeedEntity source);
Notice the names of the attributes are different.
Upvotes: 5