Skip to content

Commit dd67ce7

Browse files
authored
Merge pull request #39 from sjaakd/entity-with-key
lookup-entity-with-id
2 parents 33f2cd9 + 3c92fed commit dd67ce7

11 files changed

Lines changed: 459 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Currently, the following examples exist:
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)
2020
* _mapstruct-jpa-parent-child_: Example on how to use @Context in relation to parent / child relations in JPA)
21+
* _mapstruct-lookup-entity-with-composed-key_: Shows how an object with composite key can be read from the database in a mapping method.
2122

2223
## License
2324

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-lookup-entity-with-composed-key</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-jdk8</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.8</source>
76+
<target>1.8</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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 ArticleRepository {
26+
27+
public CombinedOfferingEntity lookup(ComposedKey key) {
28+
// do some DB lookups here.
29+
CombinedOfferingEntity entity = new CombinedOfferingEntity();
30+
entity.setKey( key );
31+
return entity;
32+
}
33+
34+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 CombinedOfferingEntity implements Entity {
26+
27+
private ComposedKey key;
28+
private String descriptionArticle1;
29+
private String descriptionArticle2;
30+
31+
@Override
32+
public ComposedKey getKey() {
33+
return key;
34+
}
35+
36+
public void setKey(ComposedKey key) {
37+
this.key = key;
38+
}
39+
40+
public String getDescriptionArticle1() {
41+
return descriptionArticle1;
42+
}
43+
44+
public void setDescriptionArticle1(String descriptionArticle1) {
45+
this.descriptionArticle1 = descriptionArticle1;
46+
}
47+
48+
public String getDescriptionArticle2() {
49+
return descriptionArticle2;
50+
}
51+
52+
public void setDescriptionArticle2(String descriptionArticle2) {
53+
this.descriptionArticle2 = descriptionArticle2;
54+
}
55+
56+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 ComposedKey {
26+
27+
private final String name1;
28+
private final String name2;
29+
30+
public ComposedKey(String name1, String name2) {
31+
this.name1 = name1;
32+
this.name2 = name2;
33+
}
34+
35+
public String getName1() {
36+
return name1;
37+
}
38+
39+
public String getName2() {
40+
return name2;
41+
}
42+
43+
44+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 interface Entity {
26+
27+
ComposedKey getKey();
28+
29+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.ObjectFactory;
25+
import org.mapstruct.TargetType;
26+
import org.mapstruct.factory.Mappers;
27+
28+
@Mapper
29+
public interface SourceTargetMapper {
30+
31+
SourceTargetMapper MAPPER = Mappers.getMapper( SourceTargetMapper.class );
32+
33+
@Mapping( target = "key", ignore = true )
34+
@Mapping( target = "descriptionArticle1", source = "brush.description" )
35+
@Mapping( target = "descriptionArticle2", source = "paste.description" )
36+
CombinedOfferingEntity toEntity(Toothbrush brush, ToothPaste paste, @Context ArticleRepository repo);
37+
38+
@ObjectFactory
39+
default <T extends Entity> T lookup(Toothbrush brush, ToothPaste paste, @Context ArticleRepository repo,
40+
@TargetType Class<T> targetType ) {
41+
ComposedKey key = new ComposedKey(brush.getName(), paste.getName() );
42+
CombinedOfferingEntity entity = repo.lookup( key );
43+
if ( entity == null ) {
44+
entity = new CombinedOfferingEntity();
45+
}
46+
return (T) entity;
47+
}
48+
49+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 ToothPaste {
26+
27+
private String name;
28+
private String description;
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public void setName(String name) {
35+
this.name = name;
36+
}
37+
38+
public String getDescription() {
39+
return description;
40+
}
41+
42+
public void setDescription(String description) {
43+
this.description = description;
44+
}
45+
46+
47+
}

0 commit comments

Comments
 (0)