-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelp.txt
More file actions
118 lines (88 loc) · 3.98 KB
/
Help.txt
File metadata and controls
118 lines (88 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
https://gemini.google.com/app/6296525182a9e7c0
https://reflectoring.io/java-mapping-with-mapstruct/
https://docs.google.com/document/d/1va_nW4pbLAdxTCLbbjgPKEtqvxiSBUS2xXo7_7CWIjg/edit?tab=t.0
---------------
https://reflectoring.io/java-mapping-with-mapstruct/
https://github.com/thombergs/code-examples/tree/master/mapstruct
https://stackabuse.com/guide-to-mapstruct-in-java-advanced-mapping-library/
https://github.com/mapstruct/mapstruct-examples/blob/main/mapstruct-lombok/src/main/java/com/mycompany/mapper/SourceTargetMapper.java
https://medium.com/javarevisited/make-your-life-easier-with-mapstruct-lombok-14225f207191
https://github.com/Milena92/mapstruct-lombok-demo/blob/main/src/main/java/com/milena/mapstructlombokdemo/mapper/AddressMapper.java
Issue:
Builder not available to other Annotation Processors
https://github.com/projectlombok/lombok/issues/1538
@AfterMapping not called for target beans with builder #1862
https://github.com/mapstruct/mapstruct/issues/1862
Clarify documentation about builders and lifecycle @AfterMapping / @BeforeMapping methods
https://github.com/mapstruct/mapstruct/issues/2719
Solution :
1) Disable @Builder
#####################################################################
---------
@Mapper(builder = @Builder(disableBuilder = true))
public interface EntityToDtoMapper {
-------------
@BeforeMapping
default void enrichOrganizationName(Department source, @MappingTarget DepartmentDto target) {
target.setOrganizationName(source.getName().toLowerCase(Locale.ROOT));
System.out.println("Before Mapping:"+target);
}
----------
@AfterMapping
default void enrichOrganizationNameWithSuffix(Department source, @MappingTarget DepartmentDto target) {
target.setOrganizationName(source.getName().toLowerCase(Locale.ROOT)+"_DEPT");
System.out.println("After Mapping:"+target);
}
--------------
Generated Impl:
--------------
@Override
public DepartmentDto toDto(Department source) {
if ( source == null ) {
return null;
}
DepartmentDto departmentDto = new DepartmentDto();
enrichOrganizationName( source, departmentDto );
Set<Employee> set = source.getEmployees();
if ( set != null ) {
departmentDto.setEmployeeName( new LinkedHashSet<Employee>( set ) );
}
departmentDto.setOrganizationName( sourceOrganizationName1( source ) );
departmentDto.setName( source.getName() );
enrichOrganizationNameWithSuffix( source, departmentDto );
return departmentDto;
}
#############################################################################################
2) @AfterMapping/@BeforeMapping annotated method must have the builder as @MappingTarget annotated parameter
#####################################################################
@Mapper
public interface EntityToDtoMapper {
@BeforeMapping
default void enrichOrganizationName(Department source, @MappingTarget DepartmentDto.DepartmentDtoBuilder target) {
target.organizationName(source.getName().toLowerCase(Locale.ROOT));
System.out.println("Before Mapping:"+target);
}
@AfterMapping
default void enrichOrganizationNameWithSuffix(Department source, @MappingTarget DepartmentDto.DepartmentDtoBuilder target) {
target.organizationName(source.getName().toLowerCase(Locale.ROOT)+"_DEPT");
System.out.println("After Mapping:"+target);
}
--------------
Generated Impl:
--------------
@Override
public DepartmentDto toDto(Department source) {
if ( source == null ) {
return null;
}
DepartmentDto.DepartmentDtoBuilder departmentDto = DepartmentDto.builder();
enrichOrganizationName( source, departmentDto );
Set<Employee> set = source.getEmployees();
if ( set != null ) {
departmentDto.employeeName( new LinkedHashSet<Employee>( set ) );
}
departmentDto.organizationName( sourceOrganizationName1( source ) );
departmentDto.name( source.getName() );
enrichOrganizationNameWithSuffix( source, departmentDto );
return departmentDto.build();
}