You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateDocumentReferenceTests.java
Copy file name to clipboardExpand all lines: src/main/asciidoc/reference/mapping.adoc
+263
Original file line number
Diff line number
Diff line change
@@ -480,6 +480,7 @@ The MappingMongoConverter can use metadata to drive the mapping of objects to do
480
480
* `@MongoId`: Applied at the field level to mark the field used for identity purpose. Accepts an optional `FieldType` to customize id conversion.
481
481
* `@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 data will be stored.
482
482
* `@DBRef`: Applied at the field to indicate it is to be stored using a com.mongodb.DBRef.
483
+
* `@DocumentReference`: Applied at the field to indicate it is to be stored as a pointer to another document. This can be a single value (the _id_ by default), or a `Document` provided via a converter.
483
484
* `@Indexed`: Applied at the field level to describe how to index the field.
484
485
* `@CompoundIndex` (repeatable): Applied at the type level to declare Compound Indexes.
485
486
* `@GeoSpatialIndexed`: Applied at the field level to describe how to geoindex the field.
@@ -826,6 +827,268 @@ Required properties that are also defined as lazy loading ``DBRef`` and used as
826
827
TIP: Lazily loaded ``DBRef``s can be hard to debug. Make sure tooling does not accidentally trigger proxy resolution by eg. calling `toString()` or some inline debug rendering invoking property getters.
827
828
Please consider to enable _trace_ logging for `org.springframework.data.mongodb.core.convert.DefaultDbRefResolver` to gain insight on `DBRef` resolution.
828
829
830
+
[[mapping-usage.linking]]
831
+
=== Using Document References
832
+
833
+
Using `@DocumentReference` offers an alternative way of linking entities in MongoDB.
834
+
While the goal is the same as when using <<mapping-usage-references,DBRefs>>, the store representation is different.
835
+
`DBRef` resolves to a document with a fixed structure as outlined in the https://docs.mongodb.com/manual/reference/database-references/[MongoDB Reference documentation]. +
836
+
Document references, do not follow a specific format.
837
+
They can be literally anything, a single value, an entire document, basically everything that can be stored in MongoDB.
838
+
By default, the mapping layer will use the referenced entities _id_ value for storage and retrieval, like in the sample below.
<1> Mark the collection of `Account` values to be linked.
880
+
<2> The mapping framework does not handle cascading saves, so make sure to persist the referenced entity individually.
881
+
<3> Add the reference to the existing entity.
882
+
<4> Linked `Account` entities are represented as an array of their `_id` values.
883
+
====
884
+
885
+
The sample above uses an `_id` based fetch query (`{ '_id' : ?#{#target} }`) for data retrieval and resolves linked entities eagerly.
886
+
It is possible to alter resolution defaults (listed below) via the attributes of `@DocumentReference`
887
+
888
+
.@DocumentReference defaults
889
+
[cols="2,3,5", options="header"]
890
+
|===
891
+
| Attribute | Description | Default
892
+
893
+
| `db`
894
+
| The target database name for collection lookup.
895
+
| The configured database provided by `MongoDatabaseFactory.getMongoDatabase()`.
896
+
897
+
| `collection`
898
+
| The target collection name.
899
+
| The annotated properties domain type, respectively the value type in case of `Collection` like or `Map` properties, collection name.
900
+
901
+
| `lookup`
902
+
| The single document lookup query. `Collection` like or `Map` properties combine individual lookups via an `$or` operator.
903
+
| An `_id` field based query (`{ '_id' : ?#{#target} }`) using the loaded source value.
904
+
905
+
| `lazy`
906
+
| If set to `true` value resolution is delayed upon first access of the property.
907
+
| Resolves properties eagerly by default.
908
+
|===
909
+
910
+
`@DocumentReference(lookup=...)` allows to define custom queries that are independent from the `_id` field and therefore offer a flexible way of defining links between entities as demonstrated in the sample below, where the `Publisher` of a book is referenced by its acronym instead of the internal `id`.
0 commit comments