我遇到一个问题,我的一个Mapstruct映射器未与 @Mapper(uses =
我们的ValidationSupportNeedMapper
从实体到DTO的映射。一个ValidationSupportNeedEntity
包含一个ActivityEntity
属性,我正试图从该属性映射到一个Activity
DTO。
因此,问题在于嵌套对象,即ActivityEntity
至Activity
。
这是源代码:
来自 ValidationSupportNeedMapper.java :
@Mapper(uses = {LifecycleErrorMessagesMapper.class, ActivityMapper.class})
public interface ValidationSupportNeedMapper {
ValidationSupportNeed toValidationSupportNeed(ValidationSupportNeedEntity source);
...
来自 ActivityMapper.java :
@Component
public class ActivityMapper {
public Activity toActivity(ActivityEntity activity) {
//Implementation
}
public ActivityEntity toActivityEntity(Activity activity) {
//Implementation
}
}
来自 ValidationSupportNeedEntity.java (实体)
public class ValidationSupportNeedEntity {
private ActivityEntity activityEntity;
来自 ValidationSupportNeed.java (DTO)
public class ValidationSupportNeed implements AutoValidated {
private Activity validationActivity;
然而,Mapstruct似乎忽略了uses=
批注上的 @Mapper
属性,并继续生成其自己的映射方法,如下所示:
@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;
}
我想念什么?有人可以帮忙吗?
编辑:ActivityMapper
未自动连接到ValidationSupportNeedMapper
实现中。
答案 0 :(得分:0)
添加映射注释可解决此问题:
@Mapping(source = "activityEntity", target = "validationActivity")
ValidationSupportNeed toValidationSupportNeed(ValidationSupportNeedEntity source);
请注意,属性名称不同。