Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MapStruct: Mapping 2 objects to a 3rd one

I have Object1 and Object2. Now, I want to map object3, with attributes from 1 & 2.

Say, I have 2 object:

1. User: {first_name, last_name, id}
2. Address: {street, locality, city, state, pin, id}

Now, with these, I want to map that in

User_View: {firstName, lastName, city, state}.

Where, first_name & last_name will be from User object and city & state from Address object.

Now, my question is, how to do that?

However, currently, I'm doing like this

@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;

            }
    }

But, here, I have to manually write the mapping in addressToView().

Therefore, is there, any way, to avoid that?

Or, what would be the preferred way, to handle such situations?

like image 993
anij Avatar asked Dec 07 '15 09:12

anij


People also ask

How do you map a list of objects using MapStruct?

In general, mapping collections with MapStruct works the same way as for simple types. Basically, we have to create a simple interface or abstract class, and declare the mapping methods. Based on our declarations, MapStruct will generate the mapping code automatically.

Is it good to use MapStruct?

All benchmarks have shown that MapStruct and JMapper are both good choices depending on the scenario.

What is MappingTarget in MapStruct?

Annotation Type MappingTargetDeclares a parameter of a mapping method to be the target of the mapping. Not more than one parameter can be declared as MappingTarget . NOTE: The parameter passed as a mapping target must not be null .

What is componentModel in MapStruct?

Enclosing class: MappingConstants public static final class MappingConstants.ComponentModel extends Object. Specifies the component model constants to which the generated mapper should adhere. It can be used with the annotation Mapper.componentModel() or MapperConfig.componentModel()


1 Answers

You can declare a mapping method with several source parameters and refer to the properties of all these parameters in your @Mapping annotations:

@Mapper
public abstract class UserViewMapper {

    @Mapping(source = "first_name", target = "user.firstName"),
    @Mapping(source = "last_name", target = "user.lastName"),
    public abstract UserView userAndAddressToView(User user, Address address);
}

As the "city" and "state" property names match in source and target, there is no need for mapping them.

Also see the chapter "Defining a mapper" in the reference documentation for more details.

like image 82
Gunnar Avatar answered Oct 08 '22 12:10

Gunnar