Allow one-to-many style lookups with via @DocumentReference.

This commit adds support for relational style One-To-Many references using a combination of ReadonlyProperty and @DocumentReference.
It allows to link types without explicitly storing the linking values within the document itself.

@Document
class Publisher {

  @Id
  ObjectId id;
  // ...

  @ReadOnlyProperty
  @DocumentReference(lookup="{'publisherId':?#{#self._id} }")
  List<Book> books;
}

Closes: #3798
Original pull request: #3802.
This commit is contained in:
Christoph Strobl
2021-09-07 11:07:27 +02:00
committed by Mark Paluch
parent dcf184888e
commit c8307d5a39
6 changed files with 218 additions and 19 deletions

View File

@@ -262,6 +262,62 @@ class Publisher {
<2> The field value placeholders of the lookup query (like `acc`) is used to form the reference document.
====
It is also possible to model relational style _One-To-Many_ references using a combination of `@ReadonlyProperty` and `@DocumentReference`.
This approach allows to link types without explicitly storing the linking values within the document itself as shown in the snipped below.
====
[source,java]
----
@Document
class Book {
@Id
ObjectId id;
String title;
List<String> author;
ObjectId publisherId; <1>
}
@Document
class Publisher {
@Id
ObjectId id;
String acronym;
String name;
@ReadOnlyProperty <2>
@DocumentReference(lookup="{'publisherId':?#{#self._id} }") <3>
List<Book> books;
}
----
.`Book` document
[source,json]
----
{
"_id" : 9a48e32,
"title" : "The Warded Man",
"author" : ["Peter V. Brett"],
"publisherId" : 8cfb002
}
----
.`Publisher` document
[source,json]
----
{
"_id" : 8cfb002,
"acronym" : "DR",
"name" : "Del Rey"
}
----
<1> Set up the link from `Book` to `Publisher` by storing the `Publisher.id` within the `Book` document.
<2> Mark the property holding the references to be read only. This prevents storing references to individual ``Book``s with the `Publisher` document.
<3> Use the `#self` variable to access values within the `Publisher` document and in this retrieve `Books` with matching `publisherId`.
====
With all the above in place it is possible to model all kind of associations between entities.
Have a look at the non-exhaustive list of samples below to get feeling for what is possible.