Skip to content

Commit af12450

Browse files
christophstroblgregturn
authored andcommitted
Add MongoDB example for linking documents.
Closes #631
1 parent a171e33 commit af12450

21 files changed

+1029
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Local Elasticsearch instance must be running to run the tests.
7272
* `gridfs` - Example project showing usage of gridFS with MongoDB.
7373
* `jmolecules` - Example of Spring Data MongoDB working with a jMolecules based domain model.
7474
* `kotlin` - Example for using [Kotlin](https://kotlinlang.org/) with MongoDB.
75+
* `linking` - Example demonstrating possibilities for linking documents.
7576
* `query-by-example` - Example project showing usage of Query by Example with MongoDB.
7677
* `querydsl` - Example project showing imperative and reactive [Querydsl](https://github.com/querydsl/querydsl) support for MongoDB.
7778
* `reactive` - Example project to show reactive template and repository support.

mongodb/linking/README.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Spring Data MongoDB - Linking Example
2+
3+
This project contains examples for linking Documents using:
4+
5+
* `@DBRef`
6+
* `@DocumentReference`
7+
8+
## DBRef
9+
10+
Use MongoDB native `dbref` for storing associations.
11+
12+
```java
13+
class Manager {
14+
15+
@DBRef
16+
private List<Employee> employees;
17+
18+
// ...
19+
}
20+
```
21+
22+
## DocumentReference
23+
24+
Link documents via their id.
25+
26+
```java
27+
class Manager {
28+
29+
@DocumentReference
30+
private List<Employee> employees;
31+
32+
// ...
33+
}
34+
```
35+
36+
Link documents via a (non _id_) property.
37+
38+
```java
39+
class Manager {
40+
41+
@DocumentReference(lookup = "{ 'name' : ?#{#target} }")
42+
private List<Employee> employees;
43+
44+
// ...
45+
}
46+
```
47+
48+
Link documents JPA style.
49+
50+
```java
51+
class Manager {
52+
53+
@ReadOnlyProperty
54+
@DocumentReference(lookup="{'managerId':?#{#self._id} }")
55+
private List<Employee> employees;
56+
57+
// ...
58+
}
59+
```
60+
----
61+
62+
For more information please refer to the [Reference Documentation](https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mapping-usage-references).

mongodb/linking/pom.xml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<artifactId>spring-data-mongodb-linking-example</artifactId>
6+
7+
<name>Spring Data MongoDB - Linking Example</name>
8+
9+
<parent>
10+
<groupId>org.springframework.data.examples</groupId>
11+
<artifactId>spring-data-mongodb-examples</artifactId>
12+
<version>2.0.0.BUILD-SNAPSHOT</version>
13+
</parent>
14+
15+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.mongodb.linking.dbref;
17+
18+
import org.springframework.boot.autoconfigure.SpringBootApplication;
19+
20+
/**
21+
* @author Christoph Strobl
22+
*/
23+
@SpringBootApplication
24+
class ApplicationConfiguration {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.mongodb.linking.dbref;
17+
18+
import org.springframework.data.annotation.Id;
19+
import org.springframework.data.mongodb.core.mapping.DBRef;
20+
21+
/**
22+
* @author Christoph Strobl
23+
*/
24+
class Employee {
25+
26+
@Id
27+
private String id;
28+
private String name;
29+
30+
@DBRef(lazy = true) // lazy to avoid stackoverflow on load
31+
private Manager manager;
32+
33+
public Employee() {
34+
}
35+
36+
public Employee(String id, String name) {
37+
this.id = id;
38+
this.name = name;
39+
}
40+
41+
public String getId() {
42+
return id;
43+
}
44+
45+
public void setId(String id) {
46+
this.id = id;
47+
}
48+
49+
public String getName() {
50+
return name;
51+
}
52+
53+
public void setName(String name) {
54+
this.name = name;
55+
}
56+
57+
public Manager getManager() {
58+
return manager;
59+
}
60+
61+
public void setManager(Manager manager) {
62+
this.manager = manager;
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.mongodb.linking.dbref;
17+
18+
import java.util.List;
19+
20+
import org.springframework.data.annotation.Id;
21+
import org.springframework.data.mongodb.core.mapping.DBRef;
22+
23+
/**
24+
* @author Christoph Strobl
25+
*/
26+
class Manager {
27+
28+
@Id //
29+
private String id;
30+
private String name;
31+
32+
@DBRef //
33+
private List<Employee> employees;
34+
35+
public Manager() {
36+
}
37+
38+
public Manager(String id, String name) {
39+
this.id = id;
40+
this.name = name;
41+
}
42+
43+
public String getId() {
44+
return id;
45+
}
46+
47+
public void setId(String id) {
48+
this.id = id;
49+
}
50+
51+
public String getName() {
52+
return name;
53+
}
54+
55+
public void setName(String name) {
56+
this.name = name;
57+
}
58+
59+
public List<Employee> getEmployees() {
60+
return employees;
61+
}
62+
63+
public void setEmployees(List<Employee> employees) {
64+
this.employees = employees;
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.mongodb.linking.docref.jpastyle;
17+
18+
import org.springframework.boot.autoconfigure.SpringBootApplication;
19+
20+
/**
21+
* @author Christoph Strobl
22+
*/
23+
@SpringBootApplication
24+
class ApplicationConfiguration {
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.mongodb.linking.docref.jpastyle;
17+
18+
import org.springframework.data.annotation.Id;
19+
20+
/**
21+
* @author Christoph Strobl
22+
*/
23+
class Employee {
24+
25+
@Id
26+
private String id;
27+
private String name;
28+
29+
/**
30+
* Only hold the id of the manager to link to it.
31+
*/
32+
private String managerId;
33+
34+
public Employee() {
35+
}
36+
37+
public Employee(String id, String name) {
38+
this.id = id;
39+
this.name = name;
40+
}
41+
42+
public String getId() {
43+
return id;
44+
}
45+
46+
public void setId(String id) {
47+
this.id = id;
48+
}
49+
50+
public String getName() {
51+
return name;
52+
}
53+
54+
public void setName(String name) {
55+
this.name = name;
56+
}
57+
58+
public String getManagerId() {
59+
return managerId;
60+
}
61+
62+
public void setManagerId(String managerId) {
63+
this.managerId = managerId;
64+
}
65+
}

0 commit comments

Comments
 (0)