From fd2dfc8a9681d0d13245dac5f78f99bbb1a8fc2e Mon Sep 17 00:00:00 2001 From: Gerrit Meier Date: Tue, 27 Oct 2020 12:32:38 +0100 Subject: [PATCH] #582 - Fix Neo4j builds. --- .../java/example/springdata/neo4j/Actor.java | 19 ++++++++-------- .../java/example/springdata/neo4j/Movie.java | 15 ------------- .../java/example/springdata/neo4j/Roles.java | 12 ++++++++++ .../neo4j/ActorRepositoryIntegrationTest.java | 22 ++++++++++--------- pom.xml | 1 + 5 files changed, 35 insertions(+), 34 deletions(-) diff --git a/neo4j/example/src/main/java/example/springdata/neo4j/Actor.java b/neo4j/example/src/main/java/example/springdata/neo4j/Actor.java index 1cfa73c6..e3494cd4 100644 --- a/neo4j/example/src/main/java/example/springdata/neo4j/Actor.java +++ b/neo4j/example/src/main/java/example/springdata/neo4j/Actor.java @@ -16,8 +16,8 @@ package example.springdata.neo4j; -import java.util.HashMap; -import java.util.Map; +import java.util.ArrayList; +import java.util.List; import org.springframework.data.neo4j.core.schema.GeneratedValue; import org.springframework.data.neo4j.core.schema.Id; @@ -36,17 +36,18 @@ public class Actor { private @Id @GeneratedValue Long id; private final String name; - private @Relationship(type = "ACTED_IN") Map roles = new HashMap<>(); + private @Relationship(type = "ACTED_IN") List roles = new ArrayList<>(); public Actor(String name) { this.name = name; } - public void actedIn(Movie movie, String roleName) { + public void actedIn(Movie movie, List roleNames) { - Roles movieRoles = this.roles.computeIfAbsent(movie, m -> new Roles()); - movieRoles.addRole(roleName); - movie.getActors().put(this, movieRoles); + Roles movieRoles = new Roles(roleNames); + movieRoles.setMovie(movie); + + this.roles.add(movieRoles); } public Long getId() { @@ -57,11 +58,11 @@ public class Actor { return name; } - public Map getRoles() { + public List getRoles() { return roles; } - public void setRoles(Map roles) { + public void setRoles(List roles) { this.roles = roles; } diff --git a/neo4j/example/src/main/java/example/springdata/neo4j/Movie.java b/neo4j/example/src/main/java/example/springdata/neo4j/Movie.java index 71f663b8..be3cdf2c 100644 --- a/neo4j/example/src/main/java/example/springdata/neo4j/Movie.java +++ b/neo4j/example/src/main/java/example/springdata/neo4j/Movie.java @@ -16,14 +16,9 @@ package example.springdata.neo4j; -import java.util.HashMap; -import java.util.Map; - import org.springframework.data.neo4j.core.schema.GeneratedValue; import org.springframework.data.neo4j.core.schema.Id; import org.springframework.data.neo4j.core.schema.Node; -import org.springframework.data.neo4j.core.schema.Relationship; -import org.springframework.data.neo4j.core.schema.Relationship.Direction; /** * A Movie node entity. @@ -38,8 +33,6 @@ public class Movie { private @Id @GeneratedValue Long id; private final String title; - private @Relationship(type = "ACTED_IN", direction = Direction.INCOMING) - Map actors = new HashMap<>(); public Movie(String title) { this.title = title; @@ -53,14 +46,6 @@ public class Movie { return title; } - public Map getActors() { - return actors; - } - - public void setActors(Map actors) { - this.actors = actors; - } - @Override public String toString() { return "Movie{" + "title='" + title + '\'' + diff --git a/neo4j/example/src/main/java/example/springdata/neo4j/Roles.java b/neo4j/example/src/main/java/example/springdata/neo4j/Roles.java index b5cc0c00..d4495af7 100644 --- a/neo4j/example/src/main/java/example/springdata/neo4j/Roles.java +++ b/neo4j/example/src/main/java/example/springdata/neo4j/Roles.java @@ -21,6 +21,7 @@ import java.util.List; import org.springframework.data.annotation.PersistenceConstructor; import org.springframework.data.neo4j.core.schema.RelationshipProperties; +import org.springframework.data.neo4j.core.schema.TargetNode; /** * A Role relationship entity between an actor and movie. @@ -33,6 +34,9 @@ public class Roles { private final List roles; + @TargetNode + private Movie movie; + public Roles() { this(Collections.emptyList()); } @@ -42,6 +46,14 @@ public class Roles { this.roles = new ArrayList<>(roles); } + public Movie getMovie() { + return movie; + } + + public void setMovie(Movie movie) { + this.movie = movie; + } + public List getRoles() { return roles; } diff --git a/neo4j/example/src/test/java/example/springdata/neo4j/ActorRepositoryIntegrationTest.java b/neo4j/example/src/test/java/example/springdata/neo4j/ActorRepositoryIntegrationTest.java index 4fd83c04..b55ac2ae 100644 --- a/neo4j/example/src/test/java/example/springdata/neo4j/ActorRepositoryIntegrationTest.java +++ b/neo4j/example/src/test/java/example/springdata/neo4j/ActorRepositoryIntegrationTest.java @@ -15,7 +15,10 @@ */ package example.springdata.neo4j; -import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.*; + +import java.util.Arrays; +import java.util.Collections; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Test; @@ -68,14 +71,15 @@ class ActorRepositoryIntegrationTest { Movie goblet = new Movie("Harry Potter and the Goblet of Fire"); Actor daniel = new Actor("Daniel Radcliffe"); - daniel.actedIn(goblet, "Harry Potter"); + daniel.actedIn(goblet, Collections.singletonList("Harry Potter")); actorRepository.save(daniel); // saves the actor and the movie assertThat(actorRepository.findById(daniel.getId())).hasValueSatisfying(actor -> { assertThat(actor.getName()).isEqualTo(daniel.getName()); assertThat(actor.getRoles()).hasSize(1) - .satisfies(roles -> assertThat(roles.values()).flatExtracting(Roles::getRoles).hasSize(1).first() + .satisfies(roles -> assertThat(roles).flatExtracting(Roles::getRoles) + .hasSize(1).first() .isEqualTo("Harry Potter")); }); } @@ -88,14 +92,12 @@ class ActorRepositoryIntegrationTest { Actor lindsayLohan = new Actor("Lindsay Lohan"); - lindsayLohan.actedIn(theParentTrap, "Hallie Parker"); - lindsayLohan.actedIn(theParentTrap, "Annie James"); - lindsayLohan.actedIn(iKnowWhoKilledMe, "Aubrey Fleming"); - lindsayLohan.actedIn(iKnowWhoKilledMe, "Dakota Moss"); + lindsayLohan.actedIn(theParentTrap, Arrays.asList("Hallie Parker","Annie James")); + lindsayLohan.actedIn(iKnowWhoKilledMe, Arrays.asList("Aubrey Fleming", "Dakota Moss")); actorRepository.save(lindsayLohan); Actor nealMcDonough = new Actor("Neal McDonough"); - nealMcDonough.actedIn(iKnowWhoKilledMe, "Daniel Fleming"); + nealMcDonough.actedIn(iKnowWhoKilledMe, Collections.singletonList("Daniel Fleming")); actorRepository.save(nealMcDonough); assertThat(actorRepository.findAllByRolesMovieTitle(iKnowWhoKilledMe.getTitle())).hasSize(2) @@ -104,10 +106,10 @@ class ActorRepositoryIntegrationTest { .allSatisfy(actor -> { if (actor.getName().equals(nealMcDonough.getName())) { assertThat(actor.getRoles()) - .allSatisfy((m, r) -> assertThat(r.getRoles()).containsOnly("Daniel Fleming")); + .allSatisfy((r) -> assertThat(r.getRoles()).containsOnly("Daniel Fleming")); } else if (actor.getName().equals(lindsayLohan.getName())) { assertThat(actor.getRoles()) - .allSatisfy((m, r) -> assertThat(r.getRoles()).containsOnly("Aubrey Fleming", "Dakota Moss")); + .allSatisfy((r) -> assertThat(r.getRoles()).containsOnly("Aubrey Fleming", "Dakota Moss")); } }); } diff --git a/pom.xml b/pom.xml index ab3b2c72..2b68ffb3 100644 --- a/pom.xml +++ b/pom.xml @@ -27,6 +27,7 @@ mongodb multi-store r2dbc + neo4j rest redis solr