Skip to content

Commit ab731f4

Browse files
gregturnodrotbohm
authored andcommitted
DATAMONGO-1019 - Corrected examples in reference documentation.
Examples were not properly converted. One table got dropped, so I added it back. Fix IMPORTANT notes. Original pull requests: #214.
1 parent d8434ff commit ab731f4

File tree

6 files changed

+238
-101
lines changed

6 files changed

+238
-101
lines changed

src/main/asciidoc/index.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
Mark Pollack, Thomas Risberg, Oliver Gierke, Costin Leau, Jon Brisbin, Thomas Darimont, Christoph Strobl
33
:toc:
44
:spring-data-commons-docs: https://raw.githubusercontent.com/spring-projects/spring-data-commons/master/src/main/asciidoc
5+
56
{version}
67

8+
© 2008-2014 The original authors.
9+
710
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._
811

912
include::preface.adoc[]

src/main/asciidoc/reference/cross-store.adoc

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ Assuming that you have a working JPA application and would like to add some cros
1010

1111
First of all you need to add a dependency on the module. Using Maven this is done by adding a dependency to your pom:
1212

13-
=== Example Maven pom.xml with spring-data-mongodb-cross-store dependency
14-
13+
.Example Maven pom.xml with spring-data-mongodb-cross-store dependency
14+
====
1515
[source,xml]
1616
----
1717
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -31,11 +31,12 @@ First of all you need to add a dependency on the module. Using Maven this is do
3131
3232
</project>
3333
----
34+
====
3435

3536
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:
3637

37-
=== Example Maven pom.xml with AspectJ plugin enabled
38-
38+
.Example Maven pom.xml with AspectJ plugin enabled
39+
====
3940
[source,xml]
4041
----
4142
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -100,11 +101,12 @@ Once this is done we need to enable AspectJ for the project. The cross-store sup
100101
101102
</project>
102103
----
104+
====
103105

104106
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:
105107

106-
=== Example application context with MongoDB and cross-store aspect support
107-
108+
.Example application context with MongoDB and cross-store aspect support
109+
====
108110
[source,xml]
109111
----
110112
<?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
150152
151153
</beans>
152154
----
155+
====
153156

154157
[[mongodb_cross-store-application]]
155158
== Writing the Cross Store Application
156159

157160
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`.
158161

159-
=== Example of Entity with @RelatedDocument
160-
162+
.Example of Entity with @RelatedDocument
163+
====
161164
[source,java]
162165
----
163166
@Entity
@@ -177,9 +180,10 @@ public class Customer {
177180
// getters and setters omitted
178181
}
179182
----
183+
====
180184

181-
=== Example of domain class to be stored as document
182-
185+
.Example of domain class to be stored as document
186+
====
183187
[source,java]
184188
----
185189
public class SurveyInfo {
@@ -208,11 +212,12 @@ public class SurveyInfo {
208212
}
209213
}
210214
----
215+
====
211216

212217
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:
213218

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+
====
216221
[source,java]
217222
----
218223
Customer customer = new Customer();
@@ -225,11 +230,12 @@ SurveyInfo surveyInfo = new SurveyInfo()
225230
customer.setSurveyInfo(surveyInfo);
226231
customerRepository.save(customer);
227232
----
233+
====
228234

229235
Executing the code above results in the following JSON document stored in MongoDB.
230236

231-
=== Example of JSON document stored in MongoDB
232-
237+
.Example of JSON document stored in MongoDB
238+
====
233239
[source,javascript]
234240
----
235241
{ "_id" : ObjectId( "4d9e8b6e3c55287f87d4b79e" ),
@@ -241,3 +247,4 @@ Executing the code above results in the following JSON document stored in MongoD
241247
"citizenship" : "Norwegian" },
242248
"_entity_field_class" : "org.springframework.data.mongodb.examples.custsvc.domain.SurveyInfo" }
243249
----
250+
====

src/main/asciidoc/reference/jmx.adoc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ The JMX support for MongoDB exposes the results of executing the 'serverStatus'
88

99
Spring's Mongo namespace enables you to easily enable JMX functionality
1010

11-
=== XML schema to configure MongoDB
12-
11+
.XML schema to configure MongoDB
12+
====
1313
[source,xml]
1414
----
1515
<?xml version="1.0" encoding="UTF-8"?>
@@ -45,6 +45,7 @@ Spring's Mongo namespace enables you to easily enable JMX functionality
4545
4646
</beans>
4747
----
48+
====
4849

4950
This will expose several MBeans
5051

src/main/asciidoc/reference/mapping.adoc

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ Unless explicitly configured, an instance of `MongoMappingConverter` is created
4343

4444
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
4545

46-
=== @Configuration class to configure MongoDB mapping support
47-
46+
.@Configuration class to configure MongoDB mapping support
47+
====
4848
[source,java]
4949
----
5050
@Configuration
@@ -83,6 +83,7 @@ public class GeoSpatialAppConfig extends AbstractMongoConfiguration {
8383
}
8484
}
8585
----
86+
====
8687

8788
`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.
8889

@@ -94,8 +95,8 @@ You can also override the method `UserCredentials getUserCredentials()` to provi
9495

9596
Spring's MongoDB namespace enables you to easily enable mapping functionality in XML
9697

97-
=== XML schema to configure MongoDB mapping support
98-
98+
.XML schema to configure MongoDB mapping support
99+
====
99100
[source,xml]
100101
----
101102
<?xml version="1.0" encoding="UTF-8"?>
@@ -134,6 +135,7 @@ Spring's MongoDB namespace enables you to easily enable mapping functionality in
134135
135136
</beans>
136137
----
138+
====
137139

138140
The `base-package` property tells it where to scan for classes annotated with the `@org.springframework.data.mongodb.core.mapping.Document` annotation.
139141

@@ -142,8 +144,8 @@ The `base-package` property tells it where to scan for classes annotated with th
142144

143145
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.
144146

145-
=== Example domain object
146-
147+
.Example domain object
148+
====
147149
[source,java]
148150
----
149151
package com.mycompany.domain;
@@ -163,17 +165,18 @@ public class Person {
163165
private String lastName;
164166
}
165167
----
168+
====
166169

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.
168171

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`.
170173

171174
[[mapping-usage-annotations]]
172175
=== Mapping annotation overview
173176

174177
The MappingMongoConverter can use metadata to drive the mapping of objects to documents. An overview of the annotations is provided below
175178

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.
177180
* `@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.
178181
* `@DBRef` - applied at the field to indicate it is to be stored using a com.mongodb.DBRef.
179182
* `@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
292295

293296
Here's an example that creates a compound index of `lastName` in ascending order and `age` in descending order:
294297

295-
==== Example Compound Index Usage
296-
298+
.Example Compound Index Usage
299+
====
297300
[source,java]
298301
----
299302
package com.mycompany.domain;
@@ -312,6 +315,7 @@ public class Person {
312315
313316
}
314317
----
318+
====
315319

316320
[[mapping-usage-indexes.text-index]]
317321
=== Text Indexes
@@ -320,8 +324,8 @@ NOTE: The text index feature is disabled by default for mongodb v.2.4.
320324

321325
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.
322326

323-
==== Example Text Index Usage
324-
327+
.Example Text Index Usage
328+
====
325329
[source,java]
326330
----
327331
@Document(language = "spanish")
@@ -340,6 +344,7 @@ class Nested {
340344
String roo;
341345
}
342346
----
347+
====
343348

344349
[[mapping-usage-references]]
345350
=== Using DBRefs
@@ -348,6 +353,7 @@ The mapping framework doesn't have to store child objects embedded within the do
348353

349354
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):
350355

356+
====
351357
[source,java]
352358
----
353359
@Document
@@ -369,10 +375,11 @@ public class Person {
369375
private List<Account> accounts;
370376
}
371377
----
378+
====
372379

373380
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.
374381

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`.
376383

377384
[[mapping-usage-events]]
378385
=== Mapping Framework Events

0 commit comments

Comments
 (0)