Skip to content

Commit 672029d

Browse files
committed
Document the usage of AggregateReference.
Closes spring-projects#934
1 parent 35589af commit 672029d

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/main/asciidoc/jdbc.adoc

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,29 @@ So, if you remove the reference, the previously referenced entity gets deleted.
239239
This also means references are 1-1 or 1-n, but not n-1 or n-m.
240240

241241
If you have n-1 or n-m references, you are, by definition, dealing with two separate aggregates.
242-
References between those should be encoded as simple `id` values, which should map properly with Spring Data JDBC.
242+
References between those may be encoded as simple `id` values, which map properly with Spring Data JDBC.
243+
A better way to encode these is to make them instances of `AggregateReference`.
244+
An `AggregateReference` is a wrapper around an id value which marks that value as a reference to a different aggregate.
245+
Also, the type of that aggregate is encoded in a type parameter.
246+
247+
248+
.Declaring and setting an `AggregateReference`
249+
====
250+
[source,java]
251+
----
252+
class Person {
253+
@Id long id;
254+
AggregateReference<Person, Long> bestFriend;
255+
}
256+
257+
// ...
258+
259+
Person p1, p2 = // some initialization
260+
261+
p1.bestFriend = AggregateReference.to(p2.id);
262+
263+
----
264+
====
243265

244266
[[jdbc.entity-persistence.custom-converters]]
245267
=== Custom converters

0 commit comments

Comments
 (0)