8

Hi, I am trying to use mapping framework in my project, but i am not able to decide which one to chose, among these there mapping framework. Selma v/s MapStruct v/s Model Mapper mapping framework ?

Please help me to chose the best feature and performance oriented framework.

  • 1
    Possible duplicate of [Java mapping: Selma vs MapStruct](https://stackoverflow.com/questions/34786737/java-mapping-selma-vs-mapstruct) – tkruse Jan 17 '18 at 02:15
  • Related list of mappers: https://stackoverflow.com/questions/1432764/ – tkruse Jan 17 '18 at 02:15

3 Answers3

10

In light of the Java 8 Stream APIs and with lombok annotation processor library, I am no longer using this mapping frameworks. I create my own mappers by implementing the Converter interface of the spring framework (package org.springframework.core.convert.converter.Converter)

    @Component
    public class ProductToProductDTO implements Converter<Product, ProductDTO> {
        @Override public ProductDTO convert( Product source ) {
            ProductDTO to = ProductDTO.builder()
                    .name( source.getName())
                    .description( source.getDescription() )
                    .tags(source.getTags().stream().map(t -> t.getTag().getLabel()).collect( Collectors.toList()))
                            .build();
            return to;
        }
    }

    @Builder //lombok annotation to create Builder class
    @Data //lombok annotation to create getters & setters
    @AllArgsConstructor //required for @Builder
    public class ProductDTO  {
        private String name;
        private String description;
        private List<String> tags;
    }
alltej
  • 5,998
  • 7
  • 39
  • 73
7

I was in similar situation before while looking for solution to leverage annotation processors. I would go with either Selma or MapStruct. Kinda similar being code generators. Played with both but went with MapStruct. Similar question here in SO can help you - Java mapping: Selma vs MapStruct

Also a project in github that benchmarked object to object mapper frameworks has Selma and MapStruct in the top - https://github.com/arey/java-object-mapper-benchmark

alltej
  • 5,998
  • 7
  • 39
  • 73
  • I was facing the same issue and the benchmark link helps a lot in making decision. – Ace Zachary Jan 17 '18 at 04:30
  • In light of the Java 8 Stream APIs, I am no longer using this mapping frameworks. I create my own mappers by implementing the `Converter` interface of the spring framework (`package org.springframework.core.convert.converter.Converter`) – alltej Jan 30 '18 at 19:50
  • As a simple justification for some of those results: it is clear that compile-time mapping class generation (like `MapStruct`) is much faster than runtime mappers that use reflection (like `ModelMapper`). – payne Sep 16 '21 at 14:11
5

You can find a complete comparation between the most used frameworks to map when you clic here. In this link you can found benchmark comparisons, how to use each framework, etc. I decided to use MapStruct because is easy to use and is so fast.

Allanh
  • 416
  • 7
  • 15