地图:如何从复制集合中的属性和集合在主实体内部的属性中跳过特定属性



我正在使用mapstruct更新现有的bean。以下是我的豆子。如您所见,我的实体bean有另一个bean的集合。

public class Entity
{
private Integer id;
private String name;
private List<AnotherEntity> anotherEntityList = new ArrayList<AnotherEntity>();
//getters & setters
}
public class AnotherEntity
{
private Integer id;
private String text;
//getters & setters
}

下面是我定义映射的方式。

Entity updateEntityWithEntity(final Entity sourceEntity, @MappingTarget final Entity targetEntity);

更新后,我希望MAPSTRUCT跳过另一个bean中的ID属性。当前,它正在清除现有集合并创建一个带有来自源值的新集合。

如果我添加以下

@Mapping(target = "anotherEntityList", ignore = true)它忽略了整个集合。但是我想要该集合,但只忽略ID属性。这样的东西。@Mapping(target = "anotherEntityList.id", ignore = true)

任何帮助都将受到赞赏!

映射无法生成列表映射。它不知道用户实际上会有什么意图。请看一下以获取更多解释。

,但假设列表均已排序,并且该元素0需要映射到目标元素0、1-1到1等。

您可以做这样的事情:

@Mapper
public interface MyMapper{
// you probably don't need the return, right?
void updateEntityWithEntity(Entity sourceEntity, @MappingTarget Entity targetEntity);
// you need to implement the list merging yourself.. MapStruct has no clue what you intent here
default updateList( List< AnotherEntity > sourceList, @MappingTarget List< AnotherEntity> targetList ) {
   for ( int i=0; i<sourceList.size(); i++ ) {
        updateAnotherEntityWithAnotherEntity( sourceList.get( i ), targetList.get( i ) );
   }
}
// used by above code in 
@Mapping( target = "id", ignore = true )
void updateAnotherEntityWithAnotherEntity(AnotherEntity sourceEntity, @MappingTarget AnotherEntity targetEntity);

}

最新更新