Thursday, November 28, 2013

Bookmark and Share

Note: This article originally appeared on mapstruct.org. In order to spread the word on MapStruct I thought it's a good idea to post it in this blog, too.

It is my great pleasure to announce the release of MapStruct 1.0.0.Alpha2.

This took us a bit longer than expected, but the new release offers quite a few exciting new features we hope you'll enjoy. The JARs have already been synched to Maven Central. The coordinates are:

Alternatively you can get a distribution bundle from SourceForge.

Beside several new mapping features (e.g. combining several sources into one target object) the release provides integration with CDI and Spring to make the retrieval of mapper objects more comfortable. We've added several new implicit data type conversions and there is now also support for converting Map objects.

Let's have a closer look at some of the additions.

Advanced mapping features

When working with data transfer objects (DTO) to pass data from the backend to the client, it is common to have one DTO which transports the data from several entities. For this purpose MapStruct supports now mapping methods with several source methods. The following shows an example:

1
2
3
4
5
6
7
8
9
@Mapper
public interface OrderMapper {

    @Mappings({
        @Mapping(source = "order.name", target = "name"),
        @Mapping(source = "houseNo", target = "houseNumber")
    })
    OrderDto orderAndAddressToOrderDto(Order order, Address deliveryAddress);
}

As for single parameter methods all attributes are mapped by name from the source objects to the target object, performing a type conversion if required. In case a property with the same name exists in more than one source object, the source parameter from which to retrieve the property must be specified using the @Mapping annotation as shown for the name property.

One of the core principles in MapStruct is type-safety. Therefore an error will be raised at generation time when such an ambiguity is not resolved. Note that when mapping a property which only exists once in the source objects to another target property name, it is optional to specify the source parameter's name.

Also related to type-safety and correctness of generated mappings is the new option to raise an error in case an attribute of the mapping target is not populated, as this typically indicates an oversight or configuration error. By default a compiler warning is created in this case. To change this e.g. into a compile error instead, the unmappedTargetPolicy property can be used like this:

1
2
3
4
5
@Mapper(unmappedTargetPolicy=ERROR)
public interface OrderMapper {

    //...
}

In some cases it is required to update an existing object with the properties from a given source object instead of instantiating a new target object. This use case can be addressed with help of the @MappingTarget annotation which denotes one method parameter as the target of the mapping like this:

1
2
3
4
5
@Mapper
public interface OrderMapper {

    void updateOrderEntityFromDto(OrderDto dto, @MappingTarget Order order);
}

Instead of instantiating a new Order object, the generated implementation of updateOrderEntityFromDto() method will update the given order instance with the attributes from the passed OrderDto.

More implicit type conversions

Several new implicit type conversions have been added for the case that the source and target type of a mapped property differ. BigDecimal and BigInteger can now automatically be converted into other numeric types as well as into String. You can finde a list of all supported conversions in the reference documentation.

Please beware of a possible value or precision loss when performing such conversions from larger to smaller numeric types. It is planned for the next milestone to optionally raise a warning in this case.

It is now also possible to convert Date into String and vice versa. For that purpose a new parameter has been added to the @Mapping annotation which allows to specify a format string as interpreted by SimpleDateFormat:

1
2
3
4
5
6
@Mapper
public interface OrderMapper {

    @Mapping(source="orderDate", dateFormat="dd.MM.yyyy HH:mm:ss")
    OrderDto orderToOrderDto(Order order);
}

Integration with CDI and Spring

The recommended way for obtaining mapper instances in the 1.0.0.Alpha1 release was to use the Mappers factory.

Alternatively it is now also possible to work with dependency injection. To make this possible, MapStruct can generate mappers which are CDI or Spring beans, based on which flavor of DI you prefer. In the following example MapStruct is adviced to make the generated mapper implementation a CDI bean by specifying "cdi" via the componentModel attribute in the @Mapper annotation:

1
2
3
4
5
@Mapper(componentModel="cdi")
public interface OrderMapper {

    //...
}

This allows to obtain an order mapper simply via @Inject (provided you have CDI enabled within your application):

1
2
@Inject
private OrderMapper orderMapper;

Note that all other mappers a generated mapper class references are also obtained via the configured component model. So if you e.g. hook in hand-written mapper classes via @Mapper#uses() make sure that these mappers are compliant with the chosen component model, e.g. are CDI beans themselves. Refer to the documentation which describes all the specifics in detail.

On a related note, if you prefer to work with the Mappers factory as before, you'll have to adapt your imports because this class has been moved to the new package org.mapstruct.factory.

Further info

This concludes our tour through the new features in MapStruct 1.0.0.Alpha2. You can find the complete list of addressed issues in the change log on GitHub. The reference documentation has been updated to cover all new functionality.

If you have any kind of feedback please make sure to let us know. Either post a comment here or open a discussion in the mapstruct-users group. Bugs and feature requests can be reported in the issue tracker and your pull request on GitHub is highly welcome! The contribution guide has all the info you need to get started with hacking on MapStruct.

Many thanks to Andreas Gudian and Lukasz Kryger who contributed to this release, that's awesome!

14 comments:

https://www.escortsguidelondon.com/district/wimbledon_escorts said...

thanks for sharing such a nice information i really appreciate your work in this and looking for more such good posts in the future thank you
Glad to visit your blog, I look forward to more good articles and I think we all like to thank so many good articles, blog to share with us.

https://www.escortsguidelondon.com/district/wimbledon_escorts said...

thanks for sharing such a nice information i really appreciate your work in this and looking for more such good posts in the future thank you
Glad to visit your blog, I look forward to more good articles and I think we all like to thank so many good articles, blog to share with us.

Speedypaper said...

Very informative article. I think that in order to get the maximum results, you need to study a lot and diligently!
Speedypaper it's writing service that helps with study.

Chloe said...

Having an assignment due tomorrow and understand you can’t do it yourself? But you’re afraid of delegating your paper to some scam services as well? Why not check https://scamfighter.net/review/essayedge.com right now?

Merlin Kristianti said...

Dengan begitu maka lawan akan tertebak cara untuk bermain. Dan kemenangan pemain akan dengan mudah untuk didapatkan.
asikqq
http://dewaqqq.club/
http://sumoqq.today/
interqq
pionpoker
bandar ceme terpercaya
freebet tanpa deposit
paito warna
syair sgp

Minat Jesisca said...

BONUS YANG TERSEDIA :
* BONUS NEW MEMBER 20%
* BONUS NEXT DEPOSIT 5% ( TERBARU )
* Bonus Turn Over Mingguan 0.5% (ALL GAME TANPA SYARAT)
* BONUS REFERAL BERJALAN TERBESAR ( BONUS DOUBLE )

AYO DAFTARKAN DIRI ANDA SEKARANG JUGA & IKUTI BERBAGAI MACAM EVENT-EVENT MENARIK LAINNYA!!

Untuk Informasi lebih lanjut silahkan hubungi :

WA : +855887950794
Line : minatpoker
Facebook : Cathy Gerald Tan

minatpoker
dewapoker
pokeronline

Agen IDN Poker Online
Bonus Poker Terbesar
Poker Online Indonesia
Situs Poker Online Terpercaya
Agen IDN Play Terbaik

Jack said...

Loking for real victoria hearts reviews? Visit my site and find a lot of customer feedback.

Repairtech Solutions said...

May I simply just say what a comfort to discover somebody who genuinely knows what they're talking about on the web. You definitely understand how to bring a problem to light and make it important. More people should read this and understand this side of the story. I was surprised that you are not more popular because you definitely have the gift. onsite mobile repair bangalore Very nice post. I absolutely love this website. Thanks! asus display replacement It’s nearly impossible to find well-informed people on this subject, however, you seem like you know what you’re talking about! Thanks huawei display repair bangalore

Repairtech Solutions said...

You need to take part in a contest for one of the greatest sites on the internet. I will recommend this web site! vivo charging port replacement A motivating discussion is definitely worth comment. I believe that you ought to publish more about this subject matter, it might not be a taboo matter but usually people don't discuss these topics. To the next! Kind regards!! lg service center Bangalore Howdy! I simply would like to give you a huge thumbs up for your excellent info you have got right here on this post. I am returning to your blog for more soon. motorola display repair bangalore

Daus Luid said...

Situs yang sudah diperiksa, dinilai, dan diputuskan layak untuk mengelola dana dari petaruh. Situs menyediakan permainan berkualitas dengan kepastian tidak ada robot yang dijalankan sebagai petaruh 98toto

Elegant IT Services said...

Great Information...Thanks for sharing the information...
Best IT and Non-IT Course Training Institute in Bangalore

bola tangkas net said...

https://www.softraid.com/forum/profile/bv88tangkasnet/

Tangkas Net merupakan sebuah permainan judi yang sangat populer semasa dilegalkannya Judi Casino di Jakarta.
Akan tetapi untuk di online sudah di resmikan langsung dari Bolavita.
Maka bagi penggemar Bola Tangkas langsung Daftar Tangkasnet
Claim langsung Bonus Freechip bagi member baru.

Kunjungi :
Website : bolavita5.com
Kontak Whatsapp : +62812-2222-995

unknown said...

HVAC & Plumbing Services
Air Star Heating guarantees reliability and quality for all equipment and services.

Air Star Heating specialists always try to deliver the most excellent quality of services to our customers at an affordable price. It is understood that every client has different needs and different problems. We try to accomplish the needs of every client according to their requests. We are having considerable experience in this field. Our specialists understand very well how things work. It doesn’t matter in which field of industry you are looking for services.
Plumbing & HVAC Services in San Diego. Call now (858) 900-9977 ✓Licensed & Insured ✓Certified Experts ✓Same Day Appointment ✓Original Parts Only ✓Warranty On Every Job.
Visit:- https://airstarheating.com

عبدالله هادى said...

شركة تنظيف موكيت بالرياض
شركة تنظيف واجهات زجاج بالرياض
شركة تنظيف شقق بالرياض
شركة عزل خزانات بالرياض