You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
NOTE: _Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically._
@@ -31,11 +31,12 @@ First of all you need to add a dependency on the module. Using Maven this is do
31
31
32
32
</project>
33
33
----
34
+
====
34
35
35
36
Once this is done we need to enable AspectJ for the project. The cross-store support is implemented using AspectJ aspects so by enabling compile time AspectJ support the cross-store features will become available to your project. In Maven you would add an additional plugin to the <build> section of the pom:
36
37
37
-
=== Example Maven pom.xml with AspectJ plugin enabled
38
-
38
+
.Example Maven pom.xml with AspectJ plugin enabled
@@ -100,11 +101,12 @@ Once this is done we need to enable AspectJ for the project. The cross-store sup
100
101
101
102
</project>
102
103
----
104
+
====
103
105
104
106
Finally, you need to configure your project to use MongoDB and also configure the aspects that are used. The following XML snippet should be added to your application context:
105
107
106
-
=== Example application context with MongoDB and cross-store aspect support
107
-
108
+
.Example application context with MongoDB and cross-store aspect support
109
+
====
108
110
[source,xml]
109
111
----
110
112
<?xml version="1.0" encoding="UTF-8"?>
@@ -150,14 +152,15 @@ Finally, you need to configure your project to use MongoDB and also configure th
150
152
151
153
</beans>
152
154
----
155
+
====
153
156
154
157
[[mongodb_cross-store-application]]
155
158
== Writing the Cross Store Application
156
159
157
160
We are assuming that you have a working JPA application so we will only cover the additional steps needed to persist part of your Entity in your Mongo database. First you need to identify the field you want persisted. It should be a domain class and follow the general rules for the Mongo mapping support covered in previous chapters. The field you want persisted in MongoDB should be annotated using the `@RelatedDocument` annotation. That is really all you need to do!. The cross-store aspects take care of the rest. This includes marking the field with `@Transient` so it won't be persisted using JPA, keeping track of any changes made to the field value and writing them to the database on successful transaction completion, loading the document from MongoDB the first time the value is used in your application. Here is an example of a simple Entity that has a field annotated with `@RelatedEntity`.
158
161
159
-
=== Example of Entity with @RelatedDocument
160
-
162
+
.Example of Entity with @RelatedDocument
163
+
====
161
164
[source,java]
162
165
----
163
166
@Entity
@@ -177,9 +180,10 @@ public class Customer {
177
180
// getters and setters omitted
178
181
}
179
182
----
183
+
====
180
184
181
-
=== Example of domain class to be stored as document
182
-
185
+
.Example of domain class to be stored as document
186
+
====
183
187
[source,java]
184
188
----
185
189
public class SurveyInfo {
@@ -208,11 +212,12 @@ public class SurveyInfo {
208
212
}
209
213
}
210
214
----
215
+
====
211
216
212
217
Once the SurveyInfo has been set on the Customer object above the MongoTemplate that was configured above is used to save the SurveyInfo along with some metadata about the JPA Entity is stored in a MongoDB collection named after the fully qualified name of the JPA Entity class. The following code:
213
218
214
-
=== Example of code using the JPA Entity configured for cross-store persistence
215
-
219
+
.Example of code using the JPA Entity configured for cross-store persistence
220
+
====
216
221
[source,java]
217
222
----
218
223
Customer customer = new Customer();
@@ -225,11 +230,12 @@ SurveyInfo surveyInfo = new SurveyInfo()
225
230
customer.setSurveyInfo(surveyInfo);
226
231
customerRepository.save(customer);
227
232
----
233
+
====
228
234
229
235
Executing the code above results in the following JSON document stored in MongoDB.
230
236
231
-
=== Example of JSON document stored in MongoDB
232
-
237
+
.Example of JSON document stored in MongoDB
238
+
====
233
239
[source,javascript]
234
240
----
235
241
{ "_id" : ObjectId( "4d9e8b6e3c55287f87d4b79e" ),
@@ -241,3 +247,4 @@ Executing the code above results in the following JSON document stored in MongoD
Copy file name to clipboardExpand all lines: src/main/asciidoc/reference/mapping.adoc
+21-14Lines changed: 21 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -43,8 +43,8 @@ Unless explicitly configured, an instance of `MongoMappingConverter` is created
43
43
44
44
You can configure the `MongoMappingConverter` as well as `com.mongodb.Mongo` and MongoTemplate either using Java or XML based metadata. Here is an example using Spring's Java based configuration
45
45
46
-
=== @Configuration class to configure MongoDB mapping support
47
-
46
+
.@Configuration class to configure MongoDB mapping support
47
+
====
48
48
[source,java]
49
49
----
50
50
@Configuration
@@ -83,6 +83,7 @@ public class GeoSpatialAppConfig extends AbstractMongoConfiguration {
83
83
}
84
84
}
85
85
----
86
+
====
86
87
87
88
`AbstractMongoConfiguration` requires you to implement methods that define a `com.mongodb.Mongo` as well as provide a database name. `AbstractMongoConfiguration` also has a method you can override named '`getMappingBasePackage`' which tells the converter where to scan for classes annotated with the `@org.springframework.data.mongodb.core.mapping.Document` annotation.
88
89
@@ -94,8 +95,8 @@ You can also override the method `UserCredentials getUserCredentials()` to provi
94
95
95
96
Spring's MongoDB namespace enables you to easily enable mapping functionality in XML
96
97
97
-
=== XML schema to configure MongoDB mapping support
98
-
98
+
.XML schema to configure MongoDB mapping support
99
+
====
99
100
[source,xml]
100
101
----
101
102
<?xml version="1.0" encoding="UTF-8"?>
@@ -134,6 +135,7 @@ Spring's MongoDB namespace enables you to easily enable mapping functionality in
134
135
135
136
</beans>
136
137
----
138
+
====
137
139
138
140
The `base-package` property tells it where to scan for classes annotated with the `@org.springframework.data.mongodb.core.mapping.Document` annotation.
139
141
@@ -142,8 +144,8 @@ The `base-package` property tells it where to scan for classes annotated with th
142
144
143
145
To take full advantage of the object mapping functionality inside the Spring Data/MongoDB support, you should annotate your mapped objects with the `@org.springframework.data.mongodb.core.mapping.Document` annotation. Although it is not necessary for the mapping framework to have this annotation (your POJOs will be mapped correctly, even without any annotations), it allows the classpath scanner to find and pre-process your domain objects to extract the necessary metadata. If you don't use this annotation, your application will take a slight performance hit the first time you store a domain object because the mapping framework needs to build up its internal metadata model so it knows about the properties of your domain object and how to persist them.
144
146
145
-
=== Example domain object
146
-
147
+
.Example domain object
148
+
====
147
149
[source,java]
148
150
----
149
151
package com.mycompany.domain;
@@ -163,17 +165,18 @@ public class Person {
163
165
private String lastName;
164
166
}
165
167
----
168
+
====
166
169
167
-
The `@Id` annotation tells the mapper which property you want to use for the MongoDB `_id` property and the `@Indexed` annotation tells the mapping framework to call `ensureIndex` on that property of your document, making searches faster.
170
+
IMPORTANT: The `@Id` annotation tells the mapper which property you want to use for the MongoDB `_id` property and the `@Indexed` annotation tells the mapping framework to call `ensureIndex` on that property of your document, making searches faster.
168
171
169
-
Automatic index creation is only done for types annotated with `@Document`.
172
+
IMPORTANT: Automatic index creation is only done for types annotated with `@Document`.
170
173
171
174
[[mapping-usage-annotations]]
172
175
=== Mapping annotation overview
173
176
174
177
The MappingMongoConverter can use metadata to drive the mapping of objects to documents. An overview of the annotations is provided below
175
178
176
-
* `@Id `- applied at the field level to mark the field used for identiy purpose.
179
+
* `@Id` - applied at the field level to mark the field used for identiy purpose.
177
180
* `@Document` - applied at the class level to indicate this class is a candidate for mapping to the database. You can specify the name of the collection where the database will be stored.
178
181
* `@DBRef` - applied at the field to indicate it is to be stored using a com.mongodb.DBRef.
179
182
* `@Indexed` - applied at the field level to describe how to index the field.
@@ -292,8 +295,8 @@ NOTE: Compound indexes are very important to improve the performance of queries
292
295
293
296
Here's an example that creates a compound index of `lastName` in ascending order and `age` in descending order:
294
297
295
-
==== Example Compound Index Usage
296
-
298
+
.Example Compound Index Usage
299
+
====
297
300
[source,java]
298
301
----
299
302
package com.mycompany.domain;
@@ -312,6 +315,7 @@ public class Person {
312
315
313
316
}
314
317
----
318
+
====
315
319
316
320
[[mapping-usage-indexes.text-index]]
317
321
=== Text Indexes
@@ -320,8 +324,8 @@ NOTE: The text index feature is disabled by default for mongodb v.2.4.
320
324
321
325
Creating a text index allows to accumulate several fields into a searchable full text index. It is only possible to have one text index per collection so all fields marked with `@TextIndexed` are combined into this index. Properties can be weighted to influence document score for ranking results. The default language for the text index is english, to change the default language set `@Document(language="spanish")` to any language you want. Using a property called `language` or `@Language` allows to define a language override on a per document base.
322
326
323
-
==== Example Text Index Usage
324
-
327
+
.Example Text Index Usage
328
+
====
325
329
[source,java]
326
330
----
327
331
@Document(language = "spanish")
@@ -340,6 +344,7 @@ class Nested {
340
344
String roo;
341
345
}
342
346
----
347
+
====
343
348
344
349
[[mapping-usage-references]]
345
350
=== Using DBRefs
@@ -348,6 +353,7 @@ The mapping framework doesn't have to store child objects embedded within the do
348
353
349
354
Here's an example of using a DBRef to refer to a specific document that exists independently of the object in which it is referenced (both classes are shown in-line for brevity's sake):
350
355
356
+
====
351
357
[source,java]
352
358
----
353
359
@Document
@@ -369,10 +375,11 @@ public class Person {
369
375
private List<Account> accounts;
370
376
}
371
377
----
378
+
====
372
379
373
380
There's no need to use something like `@OneToMany` because the mapping framework sees that you're wanting a one-to-many relationship because there is a List of objects. When the object is stored in MongoDB, there will be a list of DBRefs rather than the `Account` objects themselves.
374
381
375
-
The mapping framework does not handle cascading saves. If you change an `Account` object that is referenced by a `Person` object, you must save the Account object separately. Calling `save` on the `Person` object will not automatically save the `Account` objects in the property `accounts`.
382
+
IMPORTANT: The mapping framework does not handle cascading saves. If you change an `Account` object that is referenced by a `Person` object, you must save the Account object separately. Calling `save` on the `Person` object will not automatically save the `Account` objects in the property `accounts`.
0 commit comments