Skip to content

Commit 78235b4

Browse files
Thomas Darimontodrotbohm
Thomas Darimont
authored andcommitted
DATAMONGO-768 - Improve documentation of how to use @PersistenceConstructor.
Added an additional section to chapter 7.3 that describes the parameter value binding when the @PersistenceConstructor annotation including a small usage example. Added a concrete example for the @value annotation that uses SpEL. Original pull request: #77.
1 parent 51ece43 commit 78235b4

File tree

1 file changed

+75
-3
lines changed

1 file changed

+75
-3
lines changed

src/docbkx/reference/mapping.xml

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,11 @@ public class Person {
369369
Spring Framework . Within the mapping framework it can be applied to
370370
constructor arguments. This lets you use a Spring Expression
371371
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>
373377
</listitem>
374378

375379
<listitem>
@@ -444,7 +448,75 @@ public class Person&lt;T extends Address&gt; {
444448
// other getters/setters ommitted
445449
</programlisting>
446450

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>
448520
</section>
449521

450522
<section id="mapping-usage-indexes">
@@ -608,7 +680,7 @@ public class Person {
608680

609681
}</programlisting>
610682

611-
<para></para>
683+
<para/>
612684
</section>
613685
</section>
614686
</chapter>

0 commit comments

Comments
 (0)