From 7bb948fd62c2ac43093b73d34c13a06bfa6baa8e Mon Sep 17 00:00:00 2001 From: Dan Cebotarenco Date: Thu, 24 Aug 2023 23:03:05 +0300 Subject: [PATCH 01/10] chore(logs): enable debug --- src/main/resources/application.properties | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 98c5cc4..9406e67 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,2 @@ spring.mongodb.embedded.version=3.3.1 +logging.level.root=debug \ No newline at end of file From adab869bf170cadd0a1af1f397374e413a43a112 Mon Sep 17 00:00:00 2001 From: Dan Cebotarenco Date: Thu, 24 Aug 2023 23:05:09 +0300 Subject: [PATCH 02/10] refactor(lombok): remove noise --- src/main/java/com/example/springdatamongodb/Activity.java | 1 - src/main/java/com/example/springdatamongodb/Engagement.java | 1 - .../example/springdatamongodb/EngagementRepositoryTest.java | 5 +++-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/example/springdatamongodb/Activity.java b/src/main/java/com/example/springdatamongodb/Activity.java index 0b717a2..9c590ce 100644 --- a/src/main/java/com/example/springdatamongodb/Activity.java +++ b/src/main/java/com/example/springdatamongodb/Activity.java @@ -7,7 +7,6 @@ import org.springframework.data.mongodb.core.mapping.Document; @Data -@Builder @Document(collection = "activities") public class Activity { @Id diff --git a/src/main/java/com/example/springdatamongodb/Engagement.java b/src/main/java/com/example/springdatamongodb/Engagement.java index 8716bae..11ac880 100644 --- a/src/main/java/com/example/springdatamongodb/Engagement.java +++ b/src/main/java/com/example/springdatamongodb/Engagement.java @@ -11,7 +11,6 @@ import java.util.List; @Data -@Builder @Document(collection = "engagements") public class Engagement { diff --git a/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java b/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java index 1190d08..8ad6efa 100644 --- a/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java +++ b/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java @@ -19,11 +19,12 @@ public class EngagementRepositoryTest { @Test void should_return_engagement_with_activities() { - Engagement engagement = Engagement.builder().build(); + Engagement engagement = new Engagement(); engagementRepository.save(engagement); - Activity activity = Activity.builder().engagementId(engagement.getId()).build(); + Activity activity = new Activity(); + activity.setEngagementId(engagement.getId()); activityRepository.save(activity); Optional byId = engagementRepository.findById(engagement.getId()); From 78c21b6130ff44751e4a54f8e7b51f7055415fe4 Mon Sep 17 00:00:00 2001 From: Dan Cebotarenco Date: Thu, 24 Aug 2023 23:06:51 +0300 Subject: [PATCH 03/10] refactor(relation): model oop relation --- src/main/java/com/example/springdatamongodb/Activity.java | 2 +- .../example/springdatamongodb/EngagementRepositoryTest.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/example/springdatamongodb/Activity.java b/src/main/java/com/example/springdatamongodb/Activity.java index 9c590ce..58e1ce2 100644 --- a/src/main/java/com/example/springdatamongodb/Activity.java +++ b/src/main/java/com/example/springdatamongodb/Activity.java @@ -12,6 +12,6 @@ public class Activity { @Id private ObjectId id; - private ObjectId engagementId; + private Engagement engagement; } diff --git a/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java b/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java index 8ad6efa..106f2e2 100644 --- a/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java +++ b/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java @@ -21,10 +21,10 @@ public class EngagementRepositoryTest { void should_return_engagement_with_activities() { Engagement engagement = new Engagement(); - engagementRepository.save(engagement); + Engagement savedEngagement = engagementRepository.save(engagement); Activity activity = new Activity(); - activity.setEngagementId(engagement.getId()); + activity.setEngagement(savedEngagement); activityRepository.save(activity); Optional byId = engagementRepository.findById(engagement.getId()); From ed441f9427939321808d9841fa8d01439459b3d1 Mon Sep 17 00:00:00 2001 From: Dan Cebotarenco Date: Thu, 24 Aug 2023 23:10:02 +0300 Subject: [PATCH 04/10] refactor(relation): initialize collection null properties are excluded from `DocumentAccessor` as far as I debugged --- src/main/java/com/example/springdatamongodb/Engagement.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/example/springdatamongodb/Engagement.java b/src/main/java/com/example/springdatamongodb/Engagement.java index 11ac880..6ad26a9 100644 --- a/src/main/java/com/example/springdatamongodb/Engagement.java +++ b/src/main/java/com/example/springdatamongodb/Engagement.java @@ -8,6 +8,7 @@ import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.DocumentReference; +import java.util.ArrayList; import java.util.List; @Data @@ -19,5 +20,5 @@ public class Engagement { @ReadOnlyProperty @DocumentReference(lookup="{'engagementId':?#{#self._id}}") - private List activities; + private List activities = new ArrayList<>(); } From 7fd242bd7bf002b9f6b1d0f7240e20ef24318b75 Mon Sep 17 00:00:00 2001 From: Dan Cebotarenco Date: Thu, 24 Aug 2023 23:11:09 +0300 Subject: [PATCH 05/10] refactor(relation): enforce ManyToOne You cannot save child without the parent --- src/main/java/com/example/springdatamongodb/Activity.java | 2 ++ .../example/springdatamongodb/EngagementRepositoryTest.java | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/example/springdatamongodb/Activity.java b/src/main/java/com/example/springdatamongodb/Activity.java index 58e1ce2..399c7f3 100644 --- a/src/main/java/com/example/springdatamongodb/Activity.java +++ b/src/main/java/com/example/springdatamongodb/Activity.java @@ -2,6 +2,7 @@ import lombok.Builder; import lombok.Data; +import lombok.NonNull; import org.bson.types.ObjectId; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @@ -12,6 +13,7 @@ public class Activity { @Id private ObjectId id; + @NonNull private Engagement engagement; } diff --git a/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java b/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java index 106f2e2..9e2d59a 100644 --- a/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java +++ b/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java @@ -23,8 +23,7 @@ void should_return_engagement_with_activities() { Engagement savedEngagement = engagementRepository.save(engagement); - Activity activity = new Activity(); - activity.setEngagement(savedEngagement); + Activity activity = new Activity(savedEngagement); activityRepository.save(activity); Optional byId = engagementRepository.findById(engagement.getId()); From d39af89fb7bbf0a594b6db6c2dde8c93c62bee44 Mon Sep 17 00:00:00 2001 From: Dan Cebotarenco Date: Thu, 24 Aug 2023 23:12:11 +0300 Subject: [PATCH 06/10] refactor(relation): fix lookup --- src/main/java/com/example/springdatamongodb/Engagement.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/example/springdatamongodb/Engagement.java b/src/main/java/com/example/springdatamongodb/Engagement.java index 6ad26a9..4239e5b 100644 --- a/src/main/java/com/example/springdatamongodb/Engagement.java +++ b/src/main/java/com/example/springdatamongodb/Engagement.java @@ -19,6 +19,6 @@ public class Engagement { private ObjectId id; @ReadOnlyProperty - @DocumentReference(lookup="{'engagementId':?#{#self._id}}") + @DocumentReference(lookup="{'engagement':?#{#self._id}}") private List activities = new ArrayList<>(); } From d4a896f48f1bfe19e53337f5f6d90fa68d392fe3 Mon Sep 17 00:00:00 2001 From: Dan Cebotarenco Date: Thu, 24 Aug 2023 23:12:29 +0300 Subject: [PATCH 07/10] refactor(relation): backward link --- src/main/java/com/example/springdatamongodb/Activity.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/example/springdatamongodb/Activity.java b/src/main/java/com/example/springdatamongodb/Activity.java index 399c7f3..cf3dbc7 100644 --- a/src/main/java/com/example/springdatamongodb/Activity.java +++ b/src/main/java/com/example/springdatamongodb/Activity.java @@ -6,6 +6,7 @@ import org.bson.types.ObjectId; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; +import org.springframework.data.mongodb.core.mapping.DocumentReference; @Data @Document(collection = "activities") @@ -14,6 +15,7 @@ public class Activity { private ObjectId id; @NonNull + @DocumentReference private Engagement engagement; } From 416630f7c821c569920ba9c4c281506945850f94 Mon Sep 17 00:00:00 2001 From: Dan Cebotarenco Date: Sat, 26 Aug 2023 11:44:53 +0300 Subject: [PATCH 08/10] refactor(relation): enrich parent --- src/main/java/com/example/springdatamongodb/Activity.java | 2 ++ .../java/com/example/springdatamongodb/Engagement.java | 6 +++++- .../springdatamongodb/EngagementRepositoryTest.java | 8 +++----- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/example/springdatamongodb/Activity.java b/src/main/java/com/example/springdatamongodb/Activity.java index cf3dbc7..e640961 100644 --- a/src/main/java/com/example/springdatamongodb/Activity.java +++ b/src/main/java/com/example/springdatamongodb/Activity.java @@ -7,6 +7,7 @@ import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.DocumentReference; +import org.springframework.data.mongodb.core.mapping.Field; @Data @Document(collection = "activities") @@ -16,6 +17,7 @@ public class Activity { @NonNull @DocumentReference + @Field("engagementId") private Engagement engagement; } diff --git a/src/main/java/com/example/springdatamongodb/Engagement.java b/src/main/java/com/example/springdatamongodb/Engagement.java index 4239e5b..1c3ce07 100644 --- a/src/main/java/com/example/springdatamongodb/Engagement.java +++ b/src/main/java/com/example/springdatamongodb/Engagement.java @@ -2,6 +2,7 @@ import lombok.Builder; import lombok.Data; +import lombok.NonNull; import org.bson.types.ObjectId; import org.springframework.data.annotation.Id; import org.springframework.data.annotation.ReadOnlyProperty; @@ -18,7 +19,10 @@ public class Engagement { @Id private ObjectId id; + @NonNull + private String name; + @ReadOnlyProperty - @DocumentReference(lookup="{'engagement':?#{#self._id}}") + @DocumentReference(lookup="{'engagementId':?#{#self._id}}") private List activities = new ArrayList<>(); } diff --git a/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java b/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java index 9e2d59a..64747d1 100644 --- a/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java +++ b/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java @@ -19,11 +19,9 @@ public class EngagementRepositoryTest { @Test void should_return_engagement_with_activities() { - Engagement engagement = new Engagement(); - - Engagement savedEngagement = engagementRepository.save(engagement); - - Activity activity = new Activity(savedEngagement); + Engagement engagement = new Engagement("Foo"); + Engagement saved = engagementRepository.save(engagement); + Activity activity = new Activity(saved); activityRepository.save(activity); Optional byId = engagementRepository.findById(engagement.getId()); From 7b9b9837e33dbb6e1845a3cca07b858d9314a817 Mon Sep 17 00:00:00 2001 From: Dan Cebotarenco Date: Sat, 26 Aug 2023 11:53:10 +0300 Subject: [PATCH 09/10] refactor(relation): try cascade the parent --- .../springdatamongodb/EngagementRepositoryTest.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java b/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java index 64747d1..7b665d3 100644 --- a/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java +++ b/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java @@ -1,5 +1,6 @@ package com.example.springdatamongodb; +import org.bson.types.ObjectId; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; @@ -28,4 +29,16 @@ void should_return_engagement_with_activities() { assertThat(byId.get().getActivities()).isNotEmpty(); } + + @Test + void tryCascadeTheParent() { + Engagement engagement = new Engagement("Foo"); + engagement.setId(new ObjectId()); + Activity activity = new Activity(engagement); + activityRepository.save(activity); + + Optional byId = engagementRepository.findById(engagement.getId()); + + assertThat(byId.get().getActivities()).isEmpty(); + } } From 247d8b5795685efc327295128301aec599b39ddb Mon Sep 17 00:00:00 2001 From: Dan Cebotarenco Date: Sat, 26 Aug 2023 11:56:37 +0300 Subject: [PATCH 10/10] refactor(relation): cascade the parent without id --- .../springdatamongodb/EngagementRepositoryTest.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java b/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java index 7b665d3..5f92a66 100644 --- a/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java +++ b/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java @@ -31,9 +31,8 @@ void should_return_engagement_with_activities() { } @Test - void tryCascadeTheParent() { + void tryCascadeTheParentWithManualId() { Engagement engagement = new Engagement("Foo"); - engagement.setId(new ObjectId()); Activity activity = new Activity(engagement); activityRepository.save(activity); @@ -41,4 +40,13 @@ void tryCascadeTheParent() { assertThat(byId.get().getActivities()).isEmpty(); } + + @Test + void tryCascadeTheParentWithoutId() { + Engagement engagement = new Engagement("Foo"); + Activity activity = new Activity(engagement); + activityRepository.save(activity); + + Optional byId = engagementRepository.findById(engagement.getId()); + } }