Skip to content

Commit 55ea01d

Browse files
committed
modified nested mapping section
1 parent 5663f04 commit 55ea01d

15 files changed

Lines changed: 356 additions & 197 deletions

‎mapstruct/bin/pom.xml‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0"?>
2+
<project
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>mapstruct</artifactId>
7+
<name>mapstruct</name>
8+
<groupId>com.baeldung</groupId>
9+
<version>1.0</version>
10+
<packaging>jar</packaging>
11+
12+
<properties>
13+
<org.mapstruct.version>1.0.0.Final</org.mapstruct.version>
14+
</properties>
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.mapstruct</groupId>
18+
<artifactId>mapstruct-jdk8</artifactId>
19+
<version>${org.mapstruct.version}</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>junit</groupId>
23+
<artifactId>junit</artifactId>
24+
<version>4.12</version>
25+
<scope>test</scope>
26+
</dependency>
27+
</dependencies>
28+
<build>
29+
<finalName>mapstruct</finalName>
30+
<plugins>
31+
<plugin>
32+
<groupId>org.apache.maven.plugins</groupId>
33+
<artifactId>maven-compiler-plugin</artifactId>
34+
<version>3.5.1</version>
35+
<configuration>
36+
<source>1.8</source>
37+
<target>1.8</target>
38+
<annotationProcessorPaths>
39+
<path>
40+
<groupId>org.mapstruct</groupId>
41+
<artifactId>mapstruct-processor</artifactId>
42+
<version>${org.mapstruct.version}</version>
43+
</path>
44+
</annotationProcessorPaths>
45+
</configuration>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
</project>

‎mapstruct/pom.xml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@
2424
<version>4.12</version>
2525
<scope>test</scope>
2626
</dependency>
27+
<dependency>
28+
<groupId>org.springframework</groupId>
29+
<artifactId>spring-context</artifactId>
30+
<version>4.3.2.RELEASE</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework</groupId>
34+
<artifactId>spring-test</artifactId>
35+
<version>4.3.2.RELEASE</version>
36+
<scope>test</scope>
37+
</dependency>
2738
</dependencies>
2839
<build>
2940
<finalName>mapstruct</finalName>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.baeldung.dto;
2+
3+
public class DivisionDTO {
4+
5+
public DivisionDTO() {
6+
}
7+
8+
public DivisionDTO(int id, String name) {
9+
super();
10+
this.id = id;
11+
this.name = name;
12+
}
13+
14+
private int id;
15+
private String name;
16+
17+
public int getId() {
18+
return id;
19+
}
20+
21+
public void setId(int id) {
22+
this.id = id;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
}

‎mapstruct/src/main/java/org/baeldung/dto/EmployeeDTO.java‎

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,32 @@
22

33
public class EmployeeDTO {
44

5-
private int employeeId;
6-
private String employeeName;
7-
private int divisionId;
8-
private String divisionName;
9-
10-
public int getEmployeeId() {
11-
return employeeId;
12-
}
13-
14-
public void setEmployeeId(int employeeId) {
15-
this.employeeId = employeeId;
16-
}
17-
18-
public String getEmployeeName() {
19-
return employeeName;
20-
}
21-
22-
public void setEmployeeName(String employeeName) {
23-
this.employeeName = employeeName;
24-
}
25-
26-
public int getDivisionId() {
27-
return divisionId;
28-
}
29-
30-
public void setDivisionId(int divisionId) {
31-
this.divisionId = divisionId;
32-
}
33-
34-
public String getDivisionName() {
35-
return divisionName;
36-
}
37-
38-
public void setDivisionName(String divisionName) {
39-
this.divisionName = divisionName;
40-
}
5+
private int employeeId;
6+
private String employeeName;
7+
private DivisionDTO division;
8+
9+
public int getEmployeeId() {
10+
return employeeId;
11+
}
12+
13+
public void setEmployeeId(int employeeId) {
14+
this.employeeId = employeeId;
15+
}
16+
17+
public String getEmployeeName() {
18+
return employeeName;
19+
}
20+
21+
public void setEmployeeName(String employeeName) {
22+
this.employeeName = employeeName;
23+
}
24+
25+
public DivisionDTO getDivision() {
26+
return division;
27+
}
28+
29+
public void setDivision(DivisionDTO division) {
30+
this.division = division;
31+
}
4132

4233
}

‎mapstruct/src/main/java/org/baeldung/dto/SimpleSource.java‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22

33
public class SimpleSource {
44

5-
private String name;
6-
private String description;
7-
8-
public String getName() {
9-
return name;
10-
}
5+
private String name;
6+
private String description;
117

12-
public void setName(String name) {
13-
this.name = name;
14-
}
8+
public String getName() {
9+
return name;
10+
}
1511

16-
public String getDescription() {
17-
return description;
18-
}
12+
public void setName(String name) {
13+
this.name = name;
14+
}
15+
16+
public String getDescription() {
17+
return description;
18+
}
19+
20+
public void setDescription(String description) {
21+
this.description = description;
22+
}
1923

20-
public void setDescription(String description) {
21-
this.description = description;
22-
}
23-
2424
}

‎mapstruct/src/main/java/org/baeldung/entity/Division.java‎

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,32 @@
22

33
public class Division {
44

5-
public Division() {
6-
}
5+
public Division() {
6+
}
77

8-
public Division(int id, String name) {
9-
super();
10-
this.id = id;
11-
this.name = name;
12-
}
8+
public Division(int id, String name) {
9+
super();
10+
this.id = id;
11+
this.name = name;
12+
}
1313

14-
private int id;
15-
private String name;
14+
private int id;
15+
private String name;
1616

17-
public int getId() {
18-
return id;
19-
}
17+
public int getId() {
18+
return id;
19+
}
2020

21-
public void setId(int id) {
22-
this.id = id;
23-
}
21+
public void setId(int id) {
22+
this.id = id;
23+
}
2424

25-
public String getName() {
26-
return name;
27-
}
25+
public String getName() {
26+
return name;
27+
}
2828

29-
public void setName(String name) {
30-
this.name = name;
31-
}
29+
public void setName(String name) {
30+
this.name = name;
31+
}
3232

3333
}

‎mapstruct/src/main/java/org/baeldung/entity/Employee.java‎

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,32 @@
22

33
public class Employee {
44

5-
private int id;
6-
private String name;
7-
private Division division;
5+
private int id;
6+
private String name;
7+
private Division division;
88

9-
public int getId() {
10-
return id;
11-
}
9+
public int getId() {
10+
return id;
11+
}
1212

13-
public void setId(int id) {
14-
this.id = id;
15-
}
13+
public void setId(int id) {
14+
this.id = id;
15+
}
1616

17-
public String getName() {
18-
return name;
19-
}
17+
public String getName() {
18+
return name;
19+
}
2020

21-
public void setName(String name) {
22-
this.name = name;
23-
}
21+
public void setName(String name) {
22+
this.name = name;
23+
}
2424

25-
public Division getDivision() {
26-
return division;
27-
}
25+
public Division getDivision() {
26+
return division;
27+
}
2828

29-
public void setDivision(Division division) {
30-
this.division = division;
31-
}
29+
public void setDivision(Division division) {
30+
this.division = division;
31+
}
3232

3333
}

‎mapstruct/src/main/java/org/baeldung/entity/SimpleDestination.java‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22

33
public class SimpleDestination {
44

5-
private String name;
6-
private String description;
7-
8-
public String getName() {
9-
return name;
10-
}
5+
private String name;
6+
private String description;
117

12-
public void setName(String name) {
13-
this.name = name;
14-
}
8+
public String getName() {
9+
return name;
10+
}
1511

16-
public String getDescription() {
17-
return description;
18-
}
12+
public void setName(String name) {
13+
this.name = name;
14+
}
1915

20-
public void setDescription(String description) {
21-
this.description = description;
22-
}
16+
public String getDescription() {
17+
return description;
18+
}
19+
20+
public void setDescription(String description) {
21+
this.description = description;
22+
}
2323

2424
}
Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.baeldung.mapper;
22

3+
import org.baeldung.dto.DivisionDTO;
34
import org.baeldung.dto.EmployeeDTO;
5+
import org.baeldung.entity.Division;
46
import org.baeldung.entity.Employee;
57
import org.mapstruct.Mapper;
68
import org.mapstruct.Mapping;
@@ -9,19 +11,14 @@
911
@Mapper
1012
public interface EmployeeMapper {
1113

12-
@Mappings({
13-
@Mapping(target="divisionId",source="entity.division.id"),
14-
@Mapping(target="divisionName",source="entity.division.name"),
15-
@Mapping(target="employeeId",source="entity.id"),
16-
@Mapping(target="employeeName",source="entity.name")
17-
})
18-
EmployeeDTO employeeToEmployeeDTO(Employee entity);
19-
20-
@Mappings({
21-
@Mapping(target="id",source="dto.employeeId"),
22-
@Mapping(target="name",source="dto.employeeName"),
23-
@Mapping(target="division",expression="java(new org.baeldung.entity.Division(dto.getDivisionId(),dto.getDivisionName()))")
24-
})
25-
Employee employeeDTOtoEmployee(EmployeeDTO dto);
26-
14+
@Mappings({ @Mapping(target = "employeeId", source = "entity.id"), @Mapping(target = "employeeName", source = "entity.name") })
15+
EmployeeDTO employeeToEmployeeDTO(Employee entity);
16+
17+
@Mappings({ @Mapping(target = "id", source = "dto.employeeId"), @Mapping(target = "name", source = "dto.employeeName") })
18+
Employee employeeDTOtoEmployee(EmployeeDTO dto);
19+
20+
DivisionDTO divisionToDivisionDTO(Division entity);
21+
22+
Division divisionDTOtoDivision(DivisionDTO dto);
23+
2724
}

0 commit comments

Comments
 (0)