N Borah
N Borah

Reputation: 11

Map nested beans list : "org.mapstruct.Mapping"

I want to map the Abc class to AbcDTO using "org.mapstruct.Mapping"

class Abc {
    private List<Xyz> xyz = null;
    private String uvw;
    private String cde;
}

class AbcDTO{
    private List<XyzDTO> xyz = null;
    private String uvw;
    private String cde;
}

class Xyz{
    private String type;
    private String value;
    private String docId;
}

class  XyzDTO{
    private String type;
    private String value;
    private DocDTO document;
}

I tried to map the classes by using the annotation:

    @Mappings({
            @Mapping(source = "xyz.docId", target = "xyz.doc")
    })
 abcDTO abcToabcDTO(abc abc)

Can someone please help with how do i iterate through the nested beans and map the docId to doc? If the names are same they map automatically but I want to map from docId to doc.

Upvotes: 0

Views: 361

Answers (2)

Mohit Singh
Mohit Singh

Reputation: 534

It should be as below ( you can give it a try):

@Mappings({
    @Mapping(target="doc", source="abc.docId")
  })
AbcDTO abcToabcDTO(Abc abc);```

Upvotes: 0

Pol
Pol

Reputation: 11

when you want map list you can define it :

@Mapping(source="docId", target="doc")
XyzDTO xyzToXyzDTO(XyZ xyz);

@Mapping(source="xyz", target="xyz") //useless if two lists got same name, but good for comprehention
AbcDTO abcToAbcDTA(Abc abc);

Upvotes: 1

Related Questions