Skip to content

Commit 33f2cd9

Browse files
authored
Merge pull request #37 from sjaakd/mapstruct-jpa
mapstruct-jpa
2 parents f3bc28f + c381247 commit 33f2cd9

9 files changed

Lines changed: 420 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Currently, the following examples exist:
1717
* _mapstruct-spi-accessor-naming_: Example on how to use the Service Provider Interface (SPI) for a custom accessor naming strategy.
1818
* _mapstruct-protobuf3_: Example on how to use protobuf3 with MapStruct
1919
* _mapstruct-kotlin_: Example on how to use MapStruct with Kotlin using KAPT (Kotlin Annotation Processing Tool)
20+
* _mapstruct-jpa-parent-child_: Example on how to use @Context in relation to parent / child relations in JPA)
2021

2122
## License
2223

mapstruct-jpa-child-parent/pom.xml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
5+
and/or other contributors as indicated by the @authors tag. See the
6+
copyright.txt file in the distribution for a full listing of all
7+
contributors.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
http://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
21+
-->
22+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
23+
<modelVersion>4.0.0</modelVersion>
24+
<groupId>org.mapstruct.examples.iterable</groupId>
25+
<artifactId>mapstruct-jpa-parent-child</artifactId>
26+
<version>1.0-SNAPSHOT</version>
27+
28+
<properties>
29+
<org.mapstruct.version>1.2.0.Final</org.mapstruct.version>
30+
</properties>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>javax.persistence</groupId>
35+
<artifactId>persistence-api</artifactId>
36+
<version>1.0</version>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.mapstruct</groupId>
41+
<artifactId>mapstruct</artifactId>
42+
<version>${org.mapstruct.version}</version>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>org.mapstruct</groupId>
47+
<artifactId>mapstruct-processor</artifactId>
48+
<version>${org.mapstruct.version}</version>
49+
<scope>provided</scope>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>junit</groupId>
54+
<artifactId>junit</artifactId>
55+
<version>4.12</version>
56+
<scope>test</scope>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>org.assertj</groupId>
61+
<artifactId>assertj-core</artifactId>
62+
<version>3.9.0</version>
63+
<scope>test</scope>
64+
</dependency>
65+
</dependencies>
66+
67+
68+
<build>
69+
<plugins>
70+
<plugin>
71+
<groupId>org.apache.maven.plugins</groupId>
72+
<artifactId>maven-compiler-plugin</artifactId>
73+
<version>3.6.2</version>
74+
<configuration>
75+
<source>1.7</source>
76+
<target>1.7</target>
77+
<annotationProcessorPaths>
78+
<path>
79+
<groupId>org.mapstruct</groupId>
80+
<artifactId>mapstruct-processor</artifactId>
81+
<version>${org.mapstruct.version}</version>
82+
</path>
83+
</annotationProcessorPaths>
84+
</configuration>
85+
</plugin>
86+
</plugins>
87+
</build>
88+
89+
</project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.jpa;
20+
21+
/**
22+
*
23+
* @author Sjaak Derksen
24+
*/
25+
public class ChildDto {
26+
27+
private String name;
28+
29+
public String getName() {
30+
return name;
31+
}
32+
33+
public void setName(String name) {
34+
this.name = name;
35+
}
36+
37+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.jpa;
20+
21+
/**
22+
*
23+
* @author Sjaak Derksen
24+
*/
25+
public class ChildEntity {
26+
27+
private String name;
28+
private ParentEntity myParent;
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public void setName(String name) {
35+
this.name = name;
36+
}
37+
38+
public ParentEntity getMyParent() {
39+
return myParent;
40+
}
41+
42+
public void setMyParent(ParentEntity myParent) {
43+
this.myParent = myParent;
44+
}
45+
46+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.jpa;
20+
21+
import javax.persistence.EntityManager;
22+
import org.mapstruct.AfterMapping;
23+
import org.mapstruct.BeforeMapping;
24+
import org.mapstruct.MappingTarget;
25+
26+
/**
27+
*
28+
* @author Sjaak Derksen
29+
*/
30+
public class JpaContext {
31+
32+
private final EntityManager em;
33+
34+
private ParentEntity parentEntity;
35+
36+
public JpaContext(EntityManager em) {
37+
this.em = em;
38+
}
39+
40+
@BeforeMapping
41+
public void setEntity(@MappingTarget ParentEntity parentEntity) {
42+
this.parentEntity = parentEntity;
43+
// you could do stuff with the EntityManager here
44+
}
45+
46+
@AfterMapping
47+
public void establishRelation(@MappingTarget ChildEntity childEntity) {
48+
childEntity.setMyParent( parentEntity );
49+
// you could do stuff with the EntityManager here
50+
}
51+
52+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.jpa;
20+
21+
import java.util.List;
22+
23+
/**
24+
*
25+
* @author Sjaak Derksen
26+
*/
27+
public class ParentDto {
28+
29+
private String name;
30+
private List<ChildDto> children;
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
public List<ChildDto> getChildren() {
41+
return children;
42+
}
43+
44+
public void setChildren(List<ChildDto> children) {
45+
this.children = children;
46+
}
47+
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.jpa;
20+
21+
import java.util.List;
22+
23+
/**
24+
*
25+
* @author Sjaak Derksen
26+
*/
27+
public class ParentEntity {
28+
29+
private String name;
30+
private List<ChildEntity> children;
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
public List<ChildEntity> getChildren() {
41+
return children;
42+
}
43+
44+
public void setChildren(List<ChildEntity> children) {
45+
this.children = children;
46+
}
47+
48+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.jpa;
20+
21+
import org.mapstruct.Context;
22+
import org.mapstruct.Mapper;
23+
import org.mapstruct.Mapping;
24+
import org.mapstruct.factory.Mappers;
25+
26+
@Mapper
27+
public interface SourceTargetMapper {
28+
29+
SourceTargetMapper MAPPER = Mappers.getMapper( SourceTargetMapper.class );
30+
31+
ParentEntity toEntity(ParentDto s, @Context JpaContext ctx);
32+
33+
@Mapping(target = "myParent", ignore = true)
34+
ChildEntity toEntity(ChildDto s, @Context JpaContext ctx);
35+
}

0 commit comments

Comments
 (0)