|
191 | 191 | persisted in MongoDB should be annotated using the
|
192 | 192 | <classname>@RelatedDocument</classname> annotation. Well, that is really
|
193 | 193 | 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> |
199 | 200 |
|
200 | 201 | <example>
|
201 | 202 | <title>Example of Entity with @RelatedDocument</title>
|
@@ -226,21 +227,66 @@ public class Customer {
|
226 | 227 |
|
227 | 228 | private Map<String, String> questionsAndAnswers;
|
228 | 229 |
|
| 230 | + public SurveyInfo() { |
| 231 | + this.questionsAndAnswers = new HashMap<String, String>(); |
| 232 | + } |
| 233 | + |
| 234 | + public SurveyInfo(Map<String, String> questionsAndAnswers) { |
| 235 | + this.questionsAndAnswers = questionsAndAnswers; |
| 236 | + } |
| 237 | + |
229 | 238 | public Map<String, String> getQuestionsAndAnswers() {
|
230 | 239 | return questionsAndAnswers;
|
231 | 240 | }
|
232 | 241 |
|
233 | 242 | public void setQuestionsAndAnswers(Map<String, String> questionsAndAnswers) {
|
234 | 243 | this.questionsAndAnswers = questionsAndAnswers;
|
235 | 244 | }
|
236 |
| -} </programlisting> |
| 245 | + |
| 246 | + public SurveyInfo addQuestionAndAnswer(String question, String answer) { |
| 247 | + this.questionsAndAnswers.put(question, answer); |
| 248 | + return this; |
| 249 | + } |
| 250 | +} </programlisting> |
237 | 251 | </example>
|
238 | 252 |
|
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> |
240 | 258 |
|
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> |
242 | 274 |
|
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> |
244 | 290 |
|
245 | 291 | <para></para>
|
246 | 292 | </section>
|
|
0 commit comments