Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/one to many reversed #1

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
9 changes: 7 additions & 2 deletions src/main/java/com/example/springdatamongodb/Activity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
8 changes: 6 additions & 2 deletions src/main/java/com/example/springdatamongodb/Engagement.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Activity> activities;
private List<Activity> activities = new ArrayList<>();
}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
spring.mongodb.embedded.version=3.3.1
logging.level.root=debug
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Engagement> 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<Engagement> 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<Engagement> byId = engagementRepository.findById(engagement.getId());
}
}