{"id":134815,"date":"2025-06-23T17:58:00","date_gmt":"2025-06-23T14:58:00","guid":{"rendered":"https:\/\/www.javacodegeeks.com\/?p=134815"},"modified":"2025-06-23T11:11:10","modified_gmt":"2025-06-23T08:11:10","slug":"how-to-map-nested-properties-using-mapstruct","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/how-to-map-nested-properties-using-mapstruct.html","title":{"rendered":"How to Map Nested Properties Using MapStruct"},"content":{"rendered":"<p>This article explores how to perform nested mapping using the MapStruct library. Nested mapping is a common requirement in enterprise applications where data models are composed of complex, hierarchical structures.<\/p>\n<p><a href=\"https:\/\/mapstruct.org\/documentation\/stable\/reference\/html\/\" target=\"_blank\" rel=\"noreferrer noopener\">MapStruct<\/a> is a Java annotation processor that generates type-safe, high-performance mapping code at compile-time. It significantly reduces boilerplate code required for object mapping, making it ideal for converting between domain models and Data Transfer Objects (DTOs).<\/p>\n<p>This article demonstrates how to automatically handle collections of nested objects and implement bi-directional mapping for conversion between entities and DTOs.<\/p>\n<h2 class=\"wp-block-heading\">1. Use Case Overview<\/h2>\n<p>Imagine an application that manages music libraries. It has the following model structure:<\/p>\n<ul class=\"wp-block-list\">\n<li>A <code>Library<\/code> contains a list of <code>Song<\/code> objects.<\/li>\n<li>Each <code>Song<\/code> includes a <code>Track<\/code> object.<\/li>\n<li>The <code>Track<\/code> includes details like <code>artistName<\/code>, <code>albumName<\/code>, and <code>duration<\/code>.<\/li>\n<\/ul>\n<p>We want to map this into a flat DTO structure where:<\/p>\n<ul class=\"wp-block-list\">\n<li><code>SongDTO<\/code> includes <code>title<\/code>, <code>album<\/code>, <code>artist<\/code>, and a nested <code>TrackDTO<\/code> for <code>duration<\/code>.<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\">2. Maven Configuration<\/h2>\n<p>Below is the Maven configuration that includes the MapStruct library and the annotation processor plugin for compilation.<\/p>\n<pre class=\"brush:xml\">\n    &lt;dependencies&gt;\n        &lt;dependency&gt;\n            &lt;groupId&gt;org.mapstruct&lt;\/groupId&gt;\n            &lt;artifactId&gt;mapstruct&lt;\/artifactId&gt;\n            &lt;version&gt;1.6.3&lt;\/version&gt;\n        &lt;\/dependency&gt;\n    &lt;\/dependencies&gt;\n    &lt;build&gt;\n        &lt;finalName&gt;mapstruct-nested-mapping&lt;\/finalName&gt;\n        &lt;plugins&gt;\n            &lt;plugin&gt;\n                &lt;groupId&gt;org.apache.maven.plugins&lt;\/groupId&gt;\n                &lt;artifactId&gt;maven-compiler-plugin&lt;\/artifactId&gt;\n                &lt;version&gt;3.13.0&lt;\/version&gt;\n                &lt;configuration&gt;\n                    &lt;annotationProcessorPaths&gt;\n                        &lt;path&gt;\n                            &lt;groupId&gt;org.mapstruct&lt;\/groupId&gt;\n                            &lt;artifactId&gt;mapstruct-processor&lt;\/artifactId&gt;\n                            &lt;version&gt;1.6.3&lt;\/version&gt;\n                        &lt;\/path&gt;         \n                    &lt;\/annotationProcessorPaths&gt;\n                &lt;\/configuration&gt;\n            &lt;\/plugin&gt;\n        &lt;\/plugins&gt;\n    &lt;\/build&gt;\n<\/pre>\n<p>This configuration includes the MapStruct dependency. The annotation processor is essential for MapStruct to generate the implementation classes during compilation.<\/p>\n<h2 class=\"wp-block-heading\">3. Source Entity Classes<\/h2>\n<p>Let\u2019s define the domain model classes: <code>Library<\/code>, <code>Song<\/code>, and <code>Track<\/code>. These will represent the core domain logic in the application.<\/p>\n<pre class=\"brush:java\">\npublic class Library {\n\n    private String name;\n    private List&lt;Song&gt; songs;\n\n}\n<\/pre>\n<pre class=\"brush:java\">\npublic class Song {\n\n    private String title;\n    private Track track;\n\n}\n<\/pre>\n<pre class=\"brush:java\">\npublic class Track {\n    \n    private String artistName;\n    private String albumName;\n    private int duration; \/\/ duration in seconds\n}\n<\/pre>\n<p>These classes define a <code>Library<\/code> containing multiple <code>Song<\/code> instances. Each <code>Song<\/code> is linked to a <code>Track<\/code>, which contains metadata.<\/p>\n<h2 class=\"wp-block-heading\">4. Target DTO Classes<\/h2>\n<p>Next, we define the target Data Transfer Objects that we want to map to. <\/p>\n<pre class=\"brush:java\">\npublic class TrackDTO {\n\n    private int duration;\n}\n<\/pre>\n<p><code>TrackDTO<\/code> represents a simplified version of the <code>Track<\/code> entity, exposing only the <code>duration<\/code> field.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush:java\">\npublic class SongDTO {\n\n    private String title;\n    private String artist;\n    private String album;\n    private TrackDTO track;\n\n }\n<\/pre>\n<p><code>SongDTO<\/code> provides a flattened view of the <code>Song<\/code> entity by directly exposing the <code>artist<\/code> and <code>album<\/code> fields that are originally nested within <code>Track<\/code>. It also retains a nested <code>TrackDTO<\/code> for duration, demonstrating how MapStruct can handle both flattened and nested mappings in the same class.<\/p>\n<pre class=\"brush:java\">\npublic class LibraryDTO {\n\n    private String name;\n    private List&lt;SongDTO&gt; songs;\n}\n<\/pre>\n<p><code>LibraryDTO<\/code> serves as the top-level DTO containing a collection of <code>SongDTO<\/code> objects. It mirrors the <code>Library<\/code> entity structure.<\/p>\n<h2 class=\"wp-block-heading\">5. Mapper Interface with Nested Mappings<\/h2>\n<p>The <code>@Mapping<\/code> annotation is essential for handling cases where field names or structures differ between the source and target objects. It explicitly tells MapStruct how to map specific fields, including mapping from nested properties (e.g., <code>track.artistName<\/code>) to flattened DTO fields (e.g., <code>artist<\/code>).<\/p>\n<p>Below is the mapper interface that defines how the <code>Library<\/code>, <code>Song<\/code>, and <code>Track<\/code> entities are converted into their corresponding DTOs, and vice versa.<\/p>\n<pre class=\"brush:java\">\n@Mapper\npublic interface LibraryMapper {\n\n    LibraryMapper INSTANCE = Mappers.getMapper(LibraryMapper.class);\n\n    \/\/ Flatten nested Track to SongDTO\n    @Mapping(source = \"track.artistName\", target = \"artist\")\n    @Mapping(source = \"track.albumName\", target = \"album\")\n    SongDTO songToDto(Song song);\n\n    \/\/ Track to TrackDTO\n    TrackDTO trackToDto(Track track);\n\n    \/\/ Library to LibraryDTO (includes List&lt;Song&gt; to List&lt;SongDTO&gt;)\n    LibraryDTO libraryToDto(Library library);\n\n    \/\/ Reverse mappings\n    @Mapping(target = \"track.artistName\", source = \"artist\")\n    @Mapping(target = \"track.albumName\", source = \"album\")\n    @Mapping(target = \"track.duration\", source = \"track.duration\")\n    Song songFromDto(SongDTO dto);\n\n    Track trackFromDto(TrackDTO dto);\n\n    Library libraryFromDto(LibraryDTO dto);\n}\n<\/pre>\n<p>The <code>@Mapping<\/code> annotation is used to define explicit field-to-field mappings when the source and target field names or structures differ. For example, <code>@Mapping(source = \"track.artistName\", target = \"artist\")<\/code> maps a nested property (<code>artistName<\/code> inside <code>Track<\/code>) to a flat property (<code>artist<\/code>) in <code>SongDTO<\/code>.<\/p>\n<p>Similarly, the reverse mappings use <code>@Mapping(target = \"track.artistName\", source = \"artist\")<\/code> to reconstruct the nested <code>Track<\/code> structure from the flattened DTO. The <code>libraryToDto()<\/code> and <code>libraryFromDto()<\/code> methods automatically handle collections by recursively applying the appropriate mapping logic to the <code>List&lt;Song&gt;<\/code> and <code>List&lt;SongDTO&gt;<\/code> types.<\/p>\n<p>Finally, when the project is built, the MapStruct processor enabled through the Maven plugin automatically generates the <code>LibraryMapperImpl<\/code> class during compilation. This class contains the actual mapping logic based on the definitions provided in the <code>LibraryMapper<\/code> interface. Below is a truncated version of the generated <code>LibraryMapperImpl<\/code> class:<\/p>\n<pre class=\"brush:java\">\npublic class LibraryMapperImpl implements LibraryMapper {\n\n    @Override\n    public SongDTO songToDto(Song song) {\n        if ( song == null ) {\n            return null;\n        }\n\n        SongDTO songDTO = new SongDTO();\n\n        songDTO.setArtist( songTrackArtistName( song ) );\n        songDTO.setAlbum( songTrackAlbumName( song ) );\n        songDTO.setTitle( song.getTitle() );\n        songDTO.setTrack( trackToDto( song.getTrack() ) );\n\n        return songDTO;\n    }\n\n    @Override\n    public TrackDTO trackToDto(Track track) {\n        if ( track == null ) {\n            return null;\n        }\n\n        TrackDTO trackDTO = new TrackDTO();\n\n        trackDTO.setDuration( track.getDuration() );\n\n        return trackDTO;\n    }\n\n    @Override\n    public LibraryDTO libraryToDto(Library library) {\n        if ( library == null ) {\n            return null;\n        }\n\n        LibraryDTO libraryDTO = new LibraryDTO();\n\n        libraryDTO.setName( library.getName() );\n        libraryDTO.setSongs( songListToSongDTOList( library.getSongs() ) );\n\n        return libraryDTO;\n    }\n\n    @Override\n    public Song songFromDto(SongDTO dto) {\n        if ( dto == null ) {\n            return null;\n        }\n\n        Song song = new Song();\n\n        if ( dto.getTrack() != null ) {\n            if ( song.getTrack() == null ) {\n                song.setTrack( new Track() );\n            }\n            trackDTOToTrack( dto.getTrack(), song.getTrack() );\n        }\n        if ( song.getTrack() == null ) {\n            song.setTrack( new Track() );\n        }\n        songDTOToTrack( dto, song.getTrack() );\n        song.setTitle( dto.getTitle() );\n\n        return song;\n    }\n\n    @Override\n    public Track trackFromDto(TrackDTO dto) {\n        if ( dto == null ) {\n            return null;\n        }\n\n        Track track = new Track();\n\n        track.setDuration( dto.getDuration() );\n\n        return track;\n    }\n\n    \/\/ ...additional methods\n}\n<\/pre>\n<p>This class demonstrates how MapStruct compiles the annotated mappings into fully working Java code. Each <code>@Mapping<\/code> declared in the interface becomes a field assignment here. Note how MapStruct checks for <code>null<\/code> to avoid <code>NullPointerException<\/code>, and creates new instances of nested DTOs or entities before assigning values.<\/p>\n<h2 class=\"wp-block-heading\">6. Example Usage<\/h2>\n<p>Here\u2019s a <code>Main<\/code> class to run and see the mapper in action.<\/p>\n<pre class=\"brush:java\">\npublic class Main {\n\n    public static void main(String[] args) {\n        \/\/  Create nested Track\n        Track track = new Track();\n        track.setArtistName(\"Pink Floyd\");\n        track.setAlbumName(\"The Wall\");\n        track.setDuration(385);\n\n        \/\/ Create Song with nested Track\n        Song song = new Song();\n        song.setTitle(\"Another Brick in the Wall\");\n        song.setTrack(track);\n\n        \/\/ Create Library with nested Song\n        Library library = new Library();\n        library.setName(\"Classic Rock\");\n\n        List&lt;Song&gt; songs = new ArrayList&lt;&gt;();\n        songs.add(song);\n        library.setSongs(songs);\n\n        \/\/  Map Library to LibraryDTO \n        LibraryDTO libraryDTO = LibraryMapper.INSTANCE.libraryToDto(library);\n        System.out.println(\" Mapped to DTO:\");\n        System.out.println(\"LibraryDTO Name: \" + libraryDTO.getName());\n\n        SongDTO mappedSongDTO = libraryDTO.getSongs().get(0);\n        System.out.println(\"  SongDTO Title: \" + mappedSongDTO.getTitle());\n        System.out.println(\"  Artist (flattened): \" + mappedSongDTO.getArtist());\n        System.out.println(\"  Album (flattened): \" + mappedSongDTO.getAlbum());\n        System.out.println(\"  Duration (nested TrackDTO): \" + mappedSongDTO.getTrack().getDuration());\n\n        \/\/ Map DTO back to Library entity\n        Library mappedBackLibrary = LibraryMapper.INSTANCE.libraryFromDto(libraryDTO);\n        System.out.println(\"\\n Mapped Back to Entity:\");\n        System.out.println(\"Library Name: \" + mappedBackLibrary.getName());\n\n        Song mappedSong = mappedBackLibrary.getSongs().get(0);\n        System.out.println(\"  Song Title: \" + mappedSong.getTitle());\n\n        Track mappedTrack = mappedSong.getTrack();\n        System.out.println(\"  Artist Name: \" + mappedTrack.getArtistName());\n        System.out.println(\"  Album Name: \" + mappedTrack.getAlbumName());\n        System.out.println(\"  Duration: \" + mappedTrack.getDuration());\n    }\n}\n<\/pre>\n<p><strong>Expected Console Output<\/strong><\/p>\n<pre class=\"brush:plain\">\n Mapped to DTO:\nLibraryDTO Name: Classic Rock\n  SongDTO Title: Another Brick in the Wall\n  Artist (flattened): Pink Floyd\n  Album (flattened): The Wall\n  Duration (nested TrackDTO): 385\n\n Mapped Back to Entity:\nLibrary Name: Classic Rock\n  Song Title: Another Brick in the Wall\n  Artist Name: Pink Floyd\n  Album Name: The Wall\n  Duration: 385\n<\/pre>\n<p>This output confirms:<\/p>\n<ul class=\"wp-block-list\">\n<li><code>Track.artistName<\/code> was mapped to <code>SongDTO.artist<\/code> and then back again.<\/li>\n<li><code>Track.albumName<\/code> was mapped to <code>SongDTO.album<\/code> and then back again.<\/li>\n<li><code>Track.duration<\/code> was preserved through nested mapping via <code>TrackDTO<\/code>.<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\">7. Conclusion<\/h2>\n<p>In this article, we explored how <a href=\"https:\/\/www.javacodegeeks.com\/mapping-enums-and-strings-with-mapstruct.html\" target=\"_blank\" rel=\"noreferrer noopener\">MapStruct<\/a> effectively handles nested mappings between complex Java objects and their corresponding DTOs. By explicitly defining mappings with the <code>@Mapping<\/code> annotation, we ensured that nested properties were accurately mapped across layers.<\/p>\n<h2 class=\"wp-block-heading\">8. Download the Source Code<\/h2>\n<p>This article explored how to perform nested mapping using MapStruct.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <a href=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2025\/06\/mapstruct-nested-mapping.zip\"><strong>mapstruct nested mapping<\/strong><\/a>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This article explores how to perform nested mapping using the MapStruct library. Nested mapping is a common requirement in enterprise applications where data models are composed of complex, hierarchical structures. MapStruct is a Java annotation processor that generates type-safe, high-performance mapping code at compile-time. It significantly reduces boilerplate code required for object mapping, making it &hellip;<\/p>\n","protected":false},"author":128888,"featured_media":124163,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[4111,4108,1420,4109,4110],"class_list":["post-134815","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java","tag-mapping","tag-dto-mapping","tag-mapstruct","tag-nested-mapping","tag-object-mapping"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Map Nested Properties Using MapStruct - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Learn how to implement MapStruct nested mapping with full Java examples, DTO conversion, bidirectional mapping, and configuration setup.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/how-to-map-nested-properties-using-mapstruct.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Map Nested Properties Using MapStruct - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Learn how to implement MapStruct nested mapping with full Java examples, DTO conversion, bidirectional mapping, and configuration setup.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/how-to-map-nested-properties-using-mapstruct.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:author\" content=\"https:\/\/web.facebook.com\/omos.aziegbe\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-23T14:58:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/06\/mapstruct-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Omozegie Aziegbe\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/OAziegbe\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Omozegie Aziegbe\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/how-to-map-nested-properties-using-mapstruct.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/how-to-map-nested-properties-using-mapstruct.html\"},\"author\":{\"name\":\"Omozegie Aziegbe\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/7d3eac6e45542536e961129ae0fb453e\"},\"headline\":\"How to Map Nested Properties Using MapStruct\",\"datePublished\":\"2025-06-23T14:58:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/how-to-map-nested-properties-using-mapstruct.html\"},\"wordCount\":619,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/how-to-map-nested-properties-using-mapstruct.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2024\\\/06\\\/mapstruct-logo.jpg\",\"keywords\":[\"@Mapping\",\"DTO Mapping\",\"MapStruct\",\"Nested Mapping\",\"Object Mapping\"],\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/how-to-map-nested-properties-using-mapstruct.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/how-to-map-nested-properties-using-mapstruct.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/how-to-map-nested-properties-using-mapstruct.html\",\"name\":\"How to Map Nested Properties Using MapStruct - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/how-to-map-nested-properties-using-mapstruct.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/how-to-map-nested-properties-using-mapstruct.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2024\\\/06\\\/mapstruct-logo.jpg\",\"datePublished\":\"2025-06-23T14:58:00+00:00\",\"description\":\"Learn how to implement MapStruct nested mapping with full Java examples, DTO conversion, bidirectional mapping, and configuration setup.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/how-to-map-nested-properties-using-mapstruct.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/how-to-map-nested-properties-using-mapstruct.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/how-to-map-nested-properties-using-mapstruct.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2024\\\/06\\\/mapstruct-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2024\\\/06\\\/mapstruct-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/how-to-map-nested-properties-using-mapstruct.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/core-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"How to Map Nested Properties Using MapStruct\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/7d3eac6e45542536e961129ae0fb453e\",\"name\":\"Omozegie Aziegbe\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2023\\\/12\\\/cropped-jcg_profile_pic-96x96.jpg\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2023\\\/12\\\/cropped-jcg_profile_pic-96x96.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2023\\\/12\\\/cropped-jcg_profile_pic-96x96.jpg\",\"caption\":\"Omozegie Aziegbe\"},\"description\":\"Omos Aziegbe is a technical writer and web\\\/application developer with a BSc in Computer Science and Software Engineering from the University of Bedfordshire. Specializing in Java enterprise applications with the Jakarta EE framework, Omos also works with HTML5, CSS, and JavaScript for web development. As a freelance web developer, Omos combines technical expertise with research and writing on topics such as software engineering, programming, web application development, computer science, and technology.\",\"sameAs\":[\"https:\\\/\\\/web.facebook.com\\\/omos.aziegbe\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/omosaziegbe\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/OAziegbe\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/omozegie-aziegbe\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Map Nested Properties Using MapStruct - Java Code Geeks","description":"Learn how to implement MapStruct nested mapping with full Java examples, DTO conversion, bidirectional mapping, and configuration setup.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/how-to-map-nested-properties-using-mapstruct.html","og_locale":"en_US","og_type":"article","og_title":"How to Map Nested Properties Using MapStruct - Java Code Geeks","og_description":"Learn how to implement MapStruct nested mapping with full Java examples, DTO conversion, bidirectional mapping, and configuration setup.","og_url":"https:\/\/www.javacodegeeks.com\/how-to-map-nested-properties-using-mapstruct.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_author":"https:\/\/web.facebook.com\/omos.aziegbe","article_published_time":"2025-06-23T14:58:00+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/06\/mapstruct-logo.jpg","type":"image\/jpeg"}],"author":"Omozegie Aziegbe","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/OAziegbe","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Omozegie Aziegbe","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/how-to-map-nested-properties-using-mapstruct.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/how-to-map-nested-properties-using-mapstruct.html"},"author":{"name":"Omozegie Aziegbe","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/7d3eac6e45542536e961129ae0fb453e"},"headline":"How to Map Nested Properties Using MapStruct","datePublished":"2025-06-23T14:58:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/how-to-map-nested-properties-using-mapstruct.html"},"wordCount":619,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/how-to-map-nested-properties-using-mapstruct.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/06\/mapstruct-logo.jpg","keywords":["@Mapping","DTO Mapping","MapStruct","Nested Mapping","Object Mapping"],"articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/how-to-map-nested-properties-using-mapstruct.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/how-to-map-nested-properties-using-mapstruct.html","url":"https:\/\/www.javacodegeeks.com\/how-to-map-nested-properties-using-mapstruct.html","name":"How to Map Nested Properties Using MapStruct - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/how-to-map-nested-properties-using-mapstruct.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/how-to-map-nested-properties-using-mapstruct.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/06\/mapstruct-logo.jpg","datePublished":"2025-06-23T14:58:00+00:00","description":"Learn how to implement MapStruct nested mapping with full Java examples, DTO conversion, bidirectional mapping, and configuration setup.","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/how-to-map-nested-properties-using-mapstruct.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/how-to-map-nested-properties-using-mapstruct.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/how-to-map-nested-properties-using-mapstruct.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/06\/mapstruct-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/06\/mapstruct-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/how-to-map-nested-properties-using-mapstruct.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/core-java"},{"@type":"ListItem","position":4,"name":"How to Map Nested Properties Using MapStruct"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/7d3eac6e45542536e961129ae0fb453e","name":"Omozegie Aziegbe","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2023\/12\/cropped-jcg_profile_pic-96x96.jpg","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2023\/12\/cropped-jcg_profile_pic-96x96.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2023\/12\/cropped-jcg_profile_pic-96x96.jpg","caption":"Omozegie Aziegbe"},"description":"Omos Aziegbe is a technical writer and web\/application developer with a BSc in Computer Science and Software Engineering from the University of Bedfordshire. Specializing in Java enterprise applications with the Jakarta EE framework, Omos also works with HTML5, CSS, and JavaScript for web development. As a freelance web developer, Omos combines technical expertise with research and writing on topics such as software engineering, programming, web application development, computer science, and technology.","sameAs":["https:\/\/web.facebook.com\/omos.aziegbe","https:\/\/www.linkedin.com\/in\/omosaziegbe\/","https:\/\/x.com\/https:\/\/twitter.com\/OAziegbe"],"url":"https:\/\/www.javacodegeeks.com\/author\/omozegie-aziegbe"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/134815","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/128888"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=134815"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/134815\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/124163"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=134815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=134815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=134815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}