@@ -369,7 +369,11 @@ public class Person {
369
369
Spring Framework . Within the mapping framework it can be applied to
370
370
constructor arguments. This lets you use a Spring Expression
371
371
Language statement to transform a key's value retrieved in the
372
- database before it is used to construct a domain object.</para >
372
+ database before it is used to construct a domain object. In order to
373
+ reference a property of a given document one has to use expressions
374
+ like: <code >@Value("#root.myProperty")</code > where
375
+ <literal >root</literal > refers to the root of the given
376
+ document.</para >
373
377
</listitem >
374
378
375
379
<listitem >
@@ -444,7 +448,75 @@ public class Person<T extends Address> {
444
448
// other getters/setters ommitted
445
449
</programlisting >
446
450
447
- <para ></para >
451
+ <para />
452
+ </section >
453
+
454
+ <section id =" mapping-custom-object-construction" >
455
+ <title >Customized Object Construction</title >
456
+
457
+ <para >The Mapping Subsystem allows the customization of the object
458
+ construction by annotating a constructor with the
459
+ <literal >@PersistenceConstructor</literal > annotation. The values to be
460
+ used for the constructor parameters are resolved in the following
461
+ way:</para >
462
+
463
+ <itemizedlist >
464
+ <listitem >
465
+ <para >If a parameter is annotated with the <code >@Value</code >
466
+ annotation, the given expression is evaluated and the result is used
467
+ as the parameter value.</para >
468
+ </listitem >
469
+
470
+ <listitem >
471
+ <para >If the Java type has a property whose name matches the given
472
+ field of the input document, then it's property information is used
473
+ to select the appropriate constructor parameter to pass the input
474
+ field value to. This works only if the parameter name information is
475
+ present in the java .class files which can be achieved by compiling
476
+ the source with debug information or using the new
477
+ <literal >-parameters</literal > command-line switch for javac in Java
478
+ 8.</para >
479
+ </listitem >
480
+
481
+ <listitem >
482
+ <para >Otherwise an <classname >MappingException</classname > will be
483
+ thrown indicating that the given constructor parameter could not be
484
+ bound.</para >
485
+ </listitem >
486
+ </itemizedlist >
487
+
488
+ <programlisting language =" java" >class OrderItem {
489
+
490
+ @Id String id;
491
+ int quantity;
492
+ double unitPrice;
493
+
494
+ OrderItem(String id, @Value("#root.qty ?: 0") int quantity, double unitPrice) {
495
+ this.id = id;
496
+ this.quantity = quantity;
497
+ this.unitPrice = unitPrice;
498
+ }
499
+
500
+ // getters/setters ommitted
501
+ }
502
+
503
+ DBObject input = new BasicDBObject("id", "4711");
504
+ input.put("unitPrice", 2.5);
505
+ input.put("qty",5);
506
+ OrderItem item = converter.read(OrderItem.class, input);</programlisting >
507
+
508
+ <note >
509
+ <para >The SpEL expression in the <literal >@Value</literal > annotation
510
+ of the <literal >quantity</literal > parameter falls back to the value
511
+ <literal >0</literal > if the given property path cannot be
512
+ resolved.</para >
513
+ </note >
514
+
515
+ <para >Additional examples for using the
516
+ <classname >@PersistenceConstructor</classname > annotation can be found
517
+ in the <ulink
518
+ url =" https://github.com/spring-projects/spring-data-mongodb/blob/master/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java" >MappingMongoConverterUnitTests</ulink >
519
+ test suite.</para >
448
520
</section >
449
521
450
522
<section id =" mapping-usage-indexes" >
@@ -608,7 +680,7 @@ public class Person {
608
680
609
681
}</programlisting >
610
682
611
- <para ></ para >
683
+ <para / >
612
684
</section >
613
685
</section >
614
686
</chapter >
0 commit comments