Skip to content

Commit 486b9a0

Browse files
author
Thomas Risberg
committed
some additional cross-store docs
1 parent 8c4219b commit 486b9a0

File tree

1 file changed

+55
-9
lines changed

1 file changed

+55
-9
lines changed

src/docbkx/reference/cross-store.xml

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,12 @@
191191
persisted in MongoDB should be annotated using the
192192
<classname>@RelatedDocument</classname> annotation. Well, that is really
193193
all you need to do. The cross-store aspects take care of the rest. This
194-
include marking the field with @Transient so it won't be persisted using
195-
JPA, keeping track of any changes and flushing them on succesfull
196-
transaction completion, loading teh document for MongoDB when the values
197-
is used in your application. Here is an example of a simple Entity that
198-
has a field annotated with @RelatedEntity.</para>
194+
includes marking the field with @Transient so it won't be persisted using
195+
JPA, keeping track of any changes made to the field value and writing them
196+
to the database on succesfull transaction completion, loading teh document
197+
from MongoDB the first time the value is used in your application. Here is
198+
an example of a simple Entity that has a field annotated with
199+
@RelatedEntity.</para>
199200

200201
<example>
201202
<title>Example of Entity with @RelatedDocument</title>
@@ -226,21 +227,66 @@ public class Customer {
226227

227228
private Map&lt;String, String&gt; questionsAndAnswers;
228229

230+
public SurveyInfo() {
231+
this.questionsAndAnswers = new HashMap&lt;String, String&gt;();
232+
}
233+
234+
public SurveyInfo(Map&lt;String, String&gt; questionsAndAnswers) {
235+
this.questionsAndAnswers = questionsAndAnswers;
236+
}
237+
229238
public Map&lt;String, String&gt; getQuestionsAndAnswers() {
230239
return questionsAndAnswers;
231240
}
232241

233242
public void setQuestionsAndAnswers(Map&lt;String, String&gt; questionsAndAnswers) {
234243
this.questionsAndAnswers = questionsAndAnswers;
235244
}
236-
} </programlisting>
245+
246+
public SurveyInfo addQuestionAndAnswer(String question, String answer) {
247+
this.questionsAndAnswers.put(question, answer);
248+
return this;
249+
}
250+
} </programlisting>
237251
</example>
238252

239-
<para>Once t...</para>
253+
<para>Once the SurveyInfo has been set on the Customer object above the
254+
MongoTemplate that was configured above is used to save the SurveyInfo
255+
along with some metadata about the JPA Entity is stored in a MongoDB
256+
collection named after the fully qualified name of the JPA Entity class.
257+
The following code:</para>
240258

241-
<para></para>
259+
<example>
260+
<title>Example of code using the JPA Entity configured for cross-store
261+
persistence</title>
262+
263+
<programlisting language="java"> Customer customer = new Customer();
264+
customer.setFirstName("Sven");
265+
customer.setLastName("Olafsen");
266+
SurveyInfo surveyInfo = new SurveyInfo()
267+
.addQuestionAndAnswer("age", "22")
268+
.addQuestionAndAnswer("married", "Yes")
269+
.addQuestionAndAnswer("citizenship", "Norwegian");
270+
customer.setSurveyInfo(surveyInfo);
271+
customerRepository.save(customer);
272+
</programlisting>
273+
</example>
242274

243-
<para></para>
275+
<para>Executing the code above results in the following JSON document
276+
stored in MongoDB.</para>
277+
278+
<example>
279+
<title>Example of JSON document stored in MongoDB</title>
280+
281+
<programlisting language="javascript">{ "_id" : ObjectId( "4d9e8b6e3c55287f87d4b79e" ),
282+
"_entity_id" : 1,
283+
"_entity_class" : "org.springframework.data.mongodb.examples.custsvc.domain.Customer",
284+
"_entity_field_name" : "surveyInfo",
285+
"questionsAndAnswers" : { "married" : "Yes",
286+
"age" : "22",
287+
"citizenship" : "Norwegian" },
288+
"_entity_field_class" : "org.springframework.data.mongodb.examples.custsvc.domain.SurveyInfo" }</programlisting>
289+
</example>
244290

245291
<para></para>
246292
</section>

0 commit comments

Comments
 (0)