简体   繁体   中英

Use bidirectional entity methods with Mapstruct

I have bidirectional mapping (@OneToMany Hibernate) with extra methods to ensure that both object linked. 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.

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 . 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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