Nested Target

1,069 views
Skip to first unread message

Kevin D

unread,
Feb 23, 2016, 1:30:05 AM2/23/16
to mapstruct-users
In the mapstruct examples it shows how to take a nested source object and flatten it into a destination object.   Is there a way to easily reverse this so that the flattened object can be mapped back to to the nested object.


Example
SongNested {
   String songTitle
   Artist artist = {
       String artistName
   }
}

SongFlattened {
    String songTitle,
    String artistName
}


This is what I would write to get from the nested to the flattened
@Mapping(source="artist.artistName", target="artistName")
public SongFlattened toFlat(SongNested nested);


Is there a way to easily get from the flattened back to the nested?







Gunnar Morling

unread,
Feb 25, 2016, 4:34:55 AM2/25/16
to Kevin D, mapstruct-users
Hi Kevin,

Nesting on the target side is not supported yet.

It's a very popular request, and we've planned to look into it soon.
It's a matter of resources, essentially. The relevant issue is
https://github.com/mapstruct/mapstruct/issues/389, any help is
welcome.

--Gunnar

Kevin Dodge

unread,
Feb 26, 2016, 2:05:28 PM2/26/16
to mapstruct-users, kevin...@gmail.com
I realize that nesting on the target side is not supported.   I was looking for how someone would do the reverse mapping without that.

If I were to do it it would look like this 

public abstract SongNested toNestedSong(SongFlattened);

public abstract ArtistNested toNextedArtist(SongFlattened);

public SongNested toNested(SongFlattened flat) {
   
SongNested nested = toNestedSong(flat);

}


Kevin Dodge

unread,
Feb 26, 2016, 2:07:09 PM2/26/16
to mapstruct-users, kevin...@gmail.com
Sorry, I accidentally posted this before I was done.

public abstract SongNested toNestedSong(SongFlattened);

public abstract ArtistNested toNextedArtist(SongFlattened);

public SongNested toNested(SongFlattened
flat) {
   
SongNested nested = toNestedSong(flat);
   if (nested != null) {
      nested
.setArtist(toArtistNested(flat);
   
}
   
return nested;
}



Is there a better way than creating an abstract class and manually nesting it myself?

Sjaak Derksen

unread,
Feb 28, 2016, 7:05:47 AM2/28/16
to mapstruct-users
As in:

https://github.com/mapstruct/mapstruct/issues/389? Working on that (already for some time)..

Best regards,
Sjaak

Kevin D

unread,
Mar 7, 2016, 2:37:05 PM3/7/16
to mapstruct-users

An alternative to nesting that would work well for me would be to allow me to specify the source of a mapping as the current source object.    So to taking my previous example I would like to be able to do this --- 

@Mapping(sourceIsCurrentSource=true, target="artist")
public SongNested toNestedSong(SongFlattened song)
public ArtistNested toNextedArtist(SongFlattened);


This would allow me to pass in my flattened object as the source to another call thereby removing my need to auto wire it as I have.    Is this currently possible?   

Kevin D

unread,
Mar 7, 2016, 2:43:25 PM3/7/16
to mapstruct-users
Doing some more trial and error it turns out I can do what I want using expressions, although I would rather if there were a clearer way

@Mapping(target="artist", expression="java(toNestedAddress(song))")
public SongNested toNestedSong(SongFlattened song)
public ArtistNested toNestedArtist(SongFlattened);


Kevin D

unread,
Mar 7, 2016, 2:44:32 PM3/7/16
to mapstruct-users
Sorry, I keep mixing up what I am working on with the example.

It should have been

@Mapping(target="artist", expression="java(toNestedArtist(song))")public SongNested toNestedSong(SongFlattened song)
public ArtistNested toNestedArtist(SongFlattened);
Reply all
Reply to author
Forward
0 new messages