Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MapStruct: Difference between BeanMapping and Mapping

I have been going through the documentation of MapStruct and can't seem to figure out when the annotation @BeanMapping should be used. The documentation seems to extensively use just @Mapping for most cases as well. Could someone explain the use of @BeanMapping annotation? Perhaps with an example?

like image 696
Erric Avatar asked May 15 '26 11:05

Erric


2 Answers

The @Mapping annotation is one of the core annotation of MapStruct. It allows you to define mappings between properties that are not named the same. From the JavaDoc:

Configures the mapping of one bean attribute.

The @BeanMapping annotation is there to control the behaviour of the entire mapping method. You can use @BeanMapping to set the NullValueCheckStrategy for the entire mapping method. From the JavaDoc:

Configures the mapping between two bean types

like image 163
Filip Avatar answered May 18 '26 02:05

Filip


simply @BeanMapping let you customize the mapping process.

example 1: @BeanMapping(ignoreByDefault = true) means No automatic mapping will take place, so All mappings have to be defined manually.

example 2: @BeanMapping(ignoreUnmappedSourceProperties = {"title"}) means ignore title field during the mapping process

like image 42
mans.luv.2.code Avatar answered May 18 '26 02:05

mans.luv.2.code



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!