Java MapStruct无法映射需要外部变量的嵌套对象

Java MapStruct无法映射需要外部变量的嵌套对象,java,lombok,mapstruct,Java,Lombok,Mapstruct,我正在尝试使用MapStruct映射一个具有嵌套对象且需要外部变量的对象 源->目标映射有损,需要外部字符串 目标->源映射工作并生成输出 我使用的是Lombok,我的对象是不可变的 //实体 公共级复制{ @价值观 @建筑商 公共静态类嵌套{ @非空 私有字符串id; @可空 私有字符串版本; @非空 私有字符串外部化; } @价值观 @建筑商 公共静态类SourceEntity{ @非空 私有字符串id; @非空 私有字符串anotherId; } @价值观 @建筑商 公共静态类目标{ @

我正在尝试使用MapStruct映射一个具有嵌套对象且需要外部变量的对象

源->目标映射有损,需要外部字符串

目标->源映射工作并生成输出

我使用的是Lombok,我的对象是不可变的


//实体
公共级复制{
@价值观
@建筑商
公共静态类嵌套{
@非空
私有字符串id;
@可空
私有字符串版本;
@非空
私有字符串外部化;
}
@价值观
@建筑商
公共静态类SourceEntity{
@非空
私有字符串id;
@非空
私有字符串anotherId;
}
@价值观
@建筑商
公共静态类目标{
@非空
私有嵌套;
@非空
私有字符串anotherId;
}
}
//制图员
@制图员
公共接口重映射器{
@映射(target=“nested.version”,ignore=true)
@映射(source=“source.id”,target=“nested.id”)
@映射(source=“source.anotherId”,target=“anotherId”)
@映射(source=“externalId”,target=“nested.externalId”)
Repro.TargetEntity fromSource(Repro.SourceEntity源,字符串外部ID);
@映射(source=“nested.id”,target=“id”)
@映射(source=“anotherId”,target=“anotherId”)
从目标复制源实体(复制目标实体目标);
}
我收到错误消息(省略包名):

这告诉我要实现一个不可行的映射方法(因为它将构造一个部分
嵌套的
对象),在
build()
调用过程中会失败


有没有办法使用MapStruct解决这个问题,或者我只是实现自己的映射器?

您可以这样尝试(手写方法icm@MappingContext来传递外部ID:

@Mapper
公共接口重映射器{
@映射(target=“nested”,source=“source”)
@映射(target=“anotherId”,source=“source.anotherId”)
repo.TargetEntity fromSource(repo.SourceEntity source,@Context String externalId);
//委托给MapStruct生成的方法的默认impl
默认repo.TargetEntity.Nested resolveNested(repo.SourceEntity source,@Context String externalId){
返回委托(源,外部ID);
}
//实际映射方法,由MapStruct生成
//注意,这里externalId不是@Context注释的
@映射(target=“version”,ignore=true)
@映射(target=“id”,source=“source.id”)
@映射(target=“externalId”,source=“externalId”)
repo.TargetEntity.Nested委托(repo.SourceEntity源,字符串外部ID)
@映射(source=“nested.id”,target=“id”)
@映射(source=“anotherId”,target=“anotherId”)
从目标复制源实体(复制目标实体目标);
}

发送了一个带有工作示例的编辑,但这有效。谢谢!
Can't map property "Repro.SourceEntity source" to "Repro.Nested nested". Consider to declare/implement a mapping method: "Repro.Nested map(Repro.SourceEntity value)