diff --git a/src/main/java/com/example/springdatamongodb/Activity.java b/src/main/java/com/example/springdatamongodb/Activity.java index 0b717a2..e640961 100644 --- a/src/main/java/com/example/springdatamongodb/Activity.java +++ b/src/main/java/com/example/springdatamongodb/Activity.java @@ -2,17 +2,22 @@ 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; +import org.springframework.data.mongodb.core.mapping.DocumentReference; +import org.springframework.data.mongodb.core.mapping.Field; @Data -@Builder @Document(collection = "activities") public class Activity { @Id private ObjectId id; - private ObjectId engagementId; + @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 8716bae..1c3ce07 100644 --- a/src/main/java/com/example/springdatamongodb/Engagement.java +++ b/src/main/java/com/example/springdatamongodb/Engagement.java @@ -2,23 +2,27 @@ 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; 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 -@Builder @Document(collection = "engagements") public class Engagement { @Id private ObjectId id; + @NonNull + private String name; + @ReadOnlyProperty @DocumentReference(lookup="{'engagementId':?#{#self._id}}") - private List activities; + private List activities = new ArrayList<>(); } 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 diff --git a/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java b/src/test/java/com/example/springdatamongodb/EngagementRepositoryTest.java index 1190d08..5f92a66 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; @@ -19,15 +20,33 @@ public class EngagementRepositoryTest { @Test void should_return_engagement_with_activities() { - Engagement engagement = Engagement.builder().build(); + Engagement engagement = new Engagement("Foo"); + Engagement saved = engagementRepository.save(engagement); + Activity activity = new Activity(saved); + activityRepository.save(activity); - engagementRepository.save(engagement); + Optional byId = engagementRepository.findById(engagement.getId()); - Activity activity = Activity.builder().engagementId(engagement.getId()).build(); + assertThat(byId.get().getActivities()).isNotEmpty(); + } + + @Test + void tryCascadeTheParentWithManualId() { + Engagement engagement = new Engagement("Foo"); + Activity activity = new Activity(engagement); activityRepository.save(activity); Optional byId = engagementRepository.findById(engagement.getId()); - assertThat(byId.get().getActivities()).isNotEmpty(); + 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()); } }