MappingConstants.THROW_EXCEPTION example

In this post under MapStruct, I will explain with example the purpose of enum “MappingConstants.THROW_EXCEPTION”. This enum is mainly used when mapping source enum constant to destination enum constant. If you want to throw an exception for some source enum constant. We can use this enum constant. Below is the example showing the usage. Lets…… Continue reading MappingConstants.THROW_EXCEPTION example

MappingConstants.ANY_UNMAPPED example

In this post under MapStruct, I will explain with example the purpose of MappingConstants.ANY_UNMAPPED enum. This enum is used when mapping two enums with different constant names. It indicates MapStruct that any unmapped enum constant (i.e., source enum constant without any explicit mapping) must be mapped to a particular target enum constant. The specific target…… Continue reading MappingConstants.ANY_UNMAPPED example

Mapping two enums with different constant name

In this post under MapStruct I will show with example how to map two enums having different constant name. For our example lets say you have to map below enum Day package package12; public enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY; } to below enum Week package package12; public enum Week {…… Continue reading Mapping two enums with different constant name

Mapping two enums with same constant name

In this post under MapStruct, I will show with example how to map between two enums both having same constant name. For our example, I will use the below two enums ProcessStatus package package11; public enum ProcessStatus { RUNNING, PAUSE, CANCELLED, SHUTDOWN; } DisplayStatus package package11; public enum DisplayStatus { RUNNING, PAUSE, CANCELLED, SHUTDOWN; }…… Continue reading Mapping two enums with same constant name

Mapping flat objects to hierarchical objects

In the previous post under MapStruct I showed how to map hierarchial objects to flat objects. In this post, I will show the reverse i.e., how to map flat objects to hierarchical objects. We are assuming the property names on source and destination objects are same. For our example I will use the below model…… Continue reading Mapping flat objects to hierarchical objects

Mapping hierarchical objects to flat objects

In this post under MapStruct, I will show with example how to map hierarchical objects properties to flat object. In all my previous posts under MapStruct, the source object didn’t compose another object. In other words, there was no object hierarchy. In this post I will use a source object which has a hierarchy as…… Continue reading Mapping hierarchical objects to flat objects

Mapping to existing bean instances

In this post under MapStruct, I will explain with example how to map attributes from source object to attributes of another existing object. In all my previous post under MapStruct, when mapping attributes from source object to destination object, the Mapper framework used to create the destination object before mapping. But there may be scenarios…… Continue reading Mapping to existing bean instances

Mapping between objects with default value

In this post under MapStruct, I will show with example how to map two objects with default value. Let me further elaborate the requirement. We have “Student” class and we have to map the instance of “Student” class to an instance of “StudentDTO” class. During mapping if the value of “className” attribute in “Student” class…… Continue reading Mapping between objects with default value