简体   繁体   English

使用 Mapstruct 的双向实体方法

[英]Use bidirectional entity methods with Mapstruct

I have bidirectional mapping (@OneToMany Hibernate) with extra methods to ensure that both object linked.我有双向映射(@OneToMany Hibernate)和额外的方法来确保 object 链接。 Simple example:简单的例子:

@Setter
class ParentDto {
    List<ChildDto> childList;
}

@Setter
class ChildDto {
    String text;
}

@Setter
class Parent {

    List<Child> childList;

    public void addChild(Child child) {
        childList.add(child);
        child.setParent(this);
    }
}

@Setter
class Child {
    Parent parent;
    String text;
}

Mapper:映射器:

@Mapper(componentModel = "spring")
public interface TestMapper {

Parent toEntity(ParentDto parentDto);
}

Generated:生成:

public class TestMapperImpl implements TestMapper {

@Override
public Parent toEntity(ParentDto parentDto) {
    if ( parentDto == null ) {
        return null;
    }

    Parent parent = new Parent();
    parent.setChildList( childDtoListToChildList( parentDto.getChildList() ) );

    return parent;
}

protected Child childDtoToChild(ChildDto childDto) {
    if ( childDto == null ) {
        return null;
    }

    Child child = new Child();
    child.setText( childDto.getText() );

    return child;
}

protected List<Child> childDtoListToChildList(List<ChildDto> list) {
    if ( list == null ) {
        return null;
    }

    List<Child> list1 = new ArrayList<Child>( list.size() );
    for ( ChildDto childDto : list ) {
        list1.add( childDtoToChild( childDto ) );
    }
    return list1;
}

Main question: How to force Mapstruct to use parent.addChild (...) to keep the bi-directional mapping between parent and List of Child.主要问题:如何强制 Mapstruct 使用parent.addChild (...)来保持父级和子级列表之间的双向映射。

I have a more complex structure with multiple nested children, so extensibility would be taken into account.我有一个更复杂的结构,有多个嵌套的孩子,所以会考虑可扩展性。

MapStruct has the concept of Collection Mapping Strategies . MapStruct 具有集合映射策略的概念。 It allows you to use adders when mapping them.它允许您在映射它们时使用加法器。

eg例如

@Mapper(componentModel = "spring", collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED)
public interface TestMapper {

    Parent toEntity(ParentDto parentDto);
}

After searching for a long time, I found the best solution so far.经过长时间的搜索,我找到了迄今为止最好的解决方案。 It does not use a special method, but it allows you to maintain a bidirectonal connection.它不使用特殊方法,但它允许您保持双向连接。

@AfterMapping
default void mapBidirectional(@MappingTarget Parent parent){
    List<Child> childList = parent.getChildList();
    if (childList != null) {
        childList.forEach(child -> child.setParent(parent));
    }
}

Will be将会

@Override
public Parent toEntity(ParentDto parentDto) {
    if ( parentDto == null ) {
        return null;
    }

    Parent parent = new Parent();

    parent.setChildList( childDtoListToChildList( parentDto.getChildList() ) );

    mapBidirectional( parent );

    return parent;
}

But most likely there is another solution to this problem, since bidirectional communication is quite common, and this solution does not scale well但是这个问题很可能还有另一种解决方案,因为双向通信很常见,而且这种解决方案不能很好地扩展

And it can't be splited into several *Mapping classes, because you can't use generated variable in @AfterMapping method并且不能拆分成几个 *Mapping 类,因为你不能在 @AfterMapping 方法中使用生成的变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM