|
23 | 23 | import lombok.Data;
|
24 | 24 | import lombok.Getter;
|
25 | 25 | import lombok.Setter;
|
| 26 | +import lombok.ToString; |
26 | 27 |
|
27 | 28 | import java.util.Arrays;
|
28 | 29 | import java.util.Collections;
|
29 | 30 | import java.util.LinkedHashMap;
|
30 | 31 | import java.util.List;
|
31 | 32 | import java.util.Map;
|
32 | 33 |
|
33 |
| -import lombok.ToString; |
34 | 34 | import org.bson.Document;
|
35 | 35 | import org.junit.jupiter.api.BeforeEach;
|
36 | 36 | import org.junit.jupiter.api.Test;
|
|
39 | 39 | import org.springframework.data.annotation.Id;
|
40 | 40 | import org.springframework.data.convert.WritingConverter;
|
41 | 41 | import org.springframework.data.mongodb.core.convert.LazyLoadingTestUtils;
|
42 |
| -import org.springframework.data.mongodb.core.mapping.DBRef; |
43 | 42 | import org.springframework.data.mongodb.core.mapping.DocumentPointer;
|
44 | 43 | import org.springframework.data.mongodb.core.mapping.DocumentReference;
|
45 | 44 | import org.springframework.data.mongodb.core.mapping.Field;
|
|
52 | 51 | import com.mongodb.client.model.Filters;
|
53 | 52 |
|
54 | 53 | /**
|
55 |
| - * {@link DBRef} related integration tests for {@link MongoTemplate}. |
| 54 | + * {@link DocumentReference} related integration tests for {@link MongoTemplate}. |
56 | 55 | *
|
57 | 56 | * @author Christoph Strobl
|
58 | 57 | */
|
59 | 58 | @ExtendWith(MongoClientExtension.class)
|
60 | 59 | public class MongoTemplateDocumentReferenceTests {
|
61 | 60 |
|
62 |
| - public static final String DB_NAME = "manual-reference-tests"; |
| 61 | + public static final String DB_NAME = "document-reference-tests"; |
63 | 62 |
|
64 | 63 | static @Client MongoClient client;
|
65 | 64 |
|
@@ -492,16 +491,43 @@ void testLazyCyclic() {
|
492 | 491 |
|
493 | 492 | WithRefB b = new WithRefB();
|
494 | 493 | b.id = "b";
|
495 |
| - b.toA = a; |
496 | 494 |
|
497 | 495 | a.toB = b;
|
| 496 | + b.lazyToA = a; |
498 | 497 |
|
499 | 498 | template.save(a);
|
500 | 499 | template.save(b);
|
501 | 500 |
|
502 | 501 | WithRefA loadedA = template.query(WithRefA.class).matching(where("id").is(a.id)).firstValue();
|
503 | 502 | assertThat(loadedA).isNotNull();
|
504 | 503 | assertThat(loadedA.getToB()).isNotNull();
|
| 504 | + LazyLoadingTestUtils.assertProxy(loadedA.getToB().lazyToA, (proxy) -> { |
| 505 | + |
| 506 | + assertThat(proxy.isResolved()).isFalse(); |
| 507 | + assertThat(proxy.currentValue()).isNull(); |
| 508 | + }); |
| 509 | + } |
| 510 | + |
| 511 | + @Test |
| 512 | + void testEagerCyclic() { |
| 513 | + |
| 514 | + WithRefA a = new WithRefA(); |
| 515 | + a.id = "a"; |
| 516 | + |
| 517 | + WithRefB b = new WithRefB(); |
| 518 | + b.id = "b"; |
| 519 | + |
| 520 | + a.toB = b; |
| 521 | + b.eagerToA = a; |
| 522 | + |
| 523 | + template.save(a); |
| 524 | + template.save(b); |
| 525 | + |
| 526 | + WithRefA loadedA = template.query(WithRefA.class).matching(where("id").is(a.id)).firstValue(); |
| 527 | + |
| 528 | + assertThat(loadedA).isNotNull(); |
| 529 | + assertThat(loadedA.getToB()).isNotNull(); |
| 530 | + assertThat(loadedA.getToB().eagerToA).isSameAs(loadedA); |
505 | 531 | }
|
506 | 532 |
|
507 | 533 | @Data
|
@@ -573,7 +599,6 @@ static class SimpleObjectRef {
|
573 | 599 |
|
574 | 600 | @Id String id;
|
575 | 601 | String value;
|
576 |
| - |
577 | 602 | }
|
578 | 603 |
|
579 | 604 | @Getter
|
@@ -683,8 +708,9 @@ public Object toReference() {
|
683 | 708 | static class WithRefB/* to A */ implements ReferenceAble {
|
684 | 709 |
|
685 | 710 | @Id String id;
|
686 |
| - @DocumentReference //(lazy = true) |
687 |
| - WithRefA toA; |
| 711 | + @DocumentReference(lazy = true) WithRefA lazyToA; |
| 712 | + |
| 713 | + @DocumentReference WithRefA eagerToA; |
688 | 714 |
|
689 | 715 | @Override
|
690 | 716 | public Object toReference() {
|
|
0 commit comments