title: MapStruct: Mapping 2 objects to a 3rd one
body: <p>I have Object1 and Object2. Now, I want to map object3, with attributes from 1 &amp; 2.</p>
  
  <p>Say, I have 2 object:</p>
  
  <pre><code>1. User: {first_name, last_name, id}
  2. Address: {street, locality, city, state, pin, id}
  </code></pre>
  
  <p>Now, with these, I want to map that in</p>
  
  <pre><code>User_View: {firstName, lastName, city, state}.
  </code></pre>
  
  <p>Where, first_name &amp; last_name will be from User object 
  and city &amp; state from Address object.</p>
  
  <p><strong>Now, my question is, how to do that?</strong></p>
  
  <p>However, currently, I'm doing like this</p>
  
  <pre><code>@Mapper    
  public abstract class UserViewMapper {
          @Mappings({
                      @Mapping(source = "first_name", target = "firstName"),
                      @Mapping(source = "last_name", target = "lastName"),
                      @Mapping(target = "city", ignore = true),
                      @Mapping(target = "state", ignore = true)
  
              })
              public abstract UserView userToView(User user);
  
          public UserView addressToView(UserView userView, Address address) {
  
                  if (userView == null) {
                      return null;
                  }
  
                  if (address == null) {
                      return null;
                  }
  
                  userView.setCity(address.getCity());
                  userView.setState(address.getState()); 
  
              return userView;
  
              }
      }
  </code></pre>
  
  <p>But, here, I have to manually write the mapping in <code>addressToView()</code>.</p>
  
  <p><strong>Therefore, is there, any way, to avoid that?</strong></p>
  
  <p><strong>Or, what would be the preferred way, to handle such situations?</strong></p>