Mapstruct:使用加法器时在更新时清除集合 [英] Mapstruct: Clear Collection on update when using Adders

查看:103
本文介绍了Mapstruct:使用加法器时在更新时清除集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将DTO对象映射到我的JPA实体.我的 ParentEntity 中有一个 children 的集合.可以将它们添加 addChild().Mapstruct通过 CollectionMappingStrategy (

I try to map my DTO objects to my JPA entities. I have a Collection of children in my ParentEntity. They can be added addChild(). Using the Adder is supported by Mapstruct via the CollectionMappingStrategy (http://mapstruct.org/documentation/dev/reference/html/#collection-mapping-strategies).

如果我创建新实体,这很好用,但是在添加新子代之前,在更新时无法清除子代.

This works fine if I create new entities, but fails to clear the children on updating before adding the new children.

Mapstruct手册说( http://mapstruct.org/documentation/dev/reference/html/#updating-bean-instances ):

The Mapstruct manual says (http://mapstruct.org/documentation/dev/reference/html/#updating-bean-instances):

将清除要更新的目标bean的集合或地图类型的属性,然后使用相应的源集合或地图中的值填充.

Collection- or map-typed properties of the target bean to be updated will be cleared and then populated with the values from the corresponding source collection or map.

我想念什么?我还必须设置其他选项吗?在 https://github.com上,有一个完整的带有测试用例的示例可以重现该问题./davidfuhr/mapstruct-jpa-child-parent

What am I missing? Is there an additional option I have to set? There is a full example with test case to reproduce the problem at https://github.com/davidfuhr/mapstruct-jpa-child-parent

以下是课程:

public class ParentEntity {

    private String name;
    private List<ChildEntity> children = new ArrayList<>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<ChildEntity> getChildren() {
        return children;
    }

    public void addChild(ChildEntity child) {
        children.add(child);
        child.setMyParent(this);
    }

    public void removeChild(ChildEntity child) {
        children.remove(child);
        child.setMyParent(null);
    }

}

public class ChildEntity {

    private String name;
    private ParentEntity myParent;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ParentEntity getMyParent() {
        return myParent;
    }

    public void setMyParent(ParentEntity myParent) {
        this.myParent = myParent;
    }

}

public class ParentDto {

   private String name;
   private List<ChildDto> children;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<ChildDto> getChildren() {
        return children;
    }

    public void setChildren(List<ChildDto> children) {
        this.children = children;
    }

}

public class ChildDto {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED)
public interface SourceTargetMapper {

    SourceTargetMapper MAPPER = Mappers.getMapper(SourceTargetMapper.class);

    ParentEntity toEntity(ParentDto s);

    ParentEntity updateEntity(ParentDto s, @MappingTarget ParentEntity e);

    @Mapping(target = "myParent", ignore = true)
    ChildEntity toEntity(ChildDto s);
}

推荐答案

文档中的文本需要重新编写.问题是,特别是对于集合,没有很好的方法在MapStruct中开箱即用地处理此问题.我目前正在为文档编写一些新文本.

The text in the documentation need to be rephrased. The problem is that especially for collections, there's no good way to handle this out of the box in MapStruct. I'm currently writing some new text for the documentation.

考虑这一点(在考虑MapStruct一般应如何更新集合时):

Consider this (when thinking what MapStruct should do for updating collections in general):

  • 如果没有匹配该怎么办:不匹配的元素应该被删除吗?
  • 是否应添加不匹配的源元素?
  • 什么才算是一场比赛:等于?哈希码?比较器== 0?
  • 是否可以有多个匹配项(列表,但还取决于被视为匹配项的内容.)
  • 应如何对所得集合进行排序?
  • 是否应将新创建的对象添加到持久性上下文?
  • JPA的亲子关系如何?

关于后一个,Dali(Eclipse)还生成remove方法.那么,MapStruct是否应该根据以上情况调用它们?

About the latter one, Dali (Eclipse) also generates remove methods. So should MapStruct call these in the light of the above?

此时,它的工作方式是这样的:每当用户想要集合更新方法时,MapStruct都会对元素映射生成常规调用(而不是更新调用),因为这是唯一明智的选择.所有其余部分高度依赖用例.如果您需要事先清除集合,请使用 @BeforeMapping 清除它.

At this moment it works like this: whenever the user wants a collection update method, MapStruct generates a regular call to element mappings (in stead of an update call), because it is the only sensible thing to do. All the remainder is highly dependent on the use-case. If you need to clear the collection at before hand, use the @BeforeMapping to clear it.

注意:我只是解决了一个问题,该问题也可以用这种方式处理加法器,而不是现在得到的模糊错误消息.

Note: I just fixed an issue that handles also adders in this fashion in stead of the vague error message you get now.

如果您想要一种处理子/父关系并将其与JPA集成的好方法,请查看

If you want a nice way to handle child/parent relations and integrate them with JPA.. have a look at the examples.

这篇关于Mapstruct:使用加法器时在更新时清除集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆