From f1518d6dee6304563997a038880f6258aa301e5c Mon Sep 17 00:00:00 2001 From: Michael Simons Date: Tue, 4 Aug 2020 15:15:19 +0200 Subject: [PATCH] DATAGRAPH-1350 - Incoming relationships are not treated correctly when using custom queries. This change checks the direction of the relationship definition and based on that selects the corresponding target node id. It also uses the property accessor to retrieve the changed instance. Furthermore the opportunity is used for some polishing in the pom. --- pom.xml | 2 - .../core/mapping/DefaultNeo4jConverter.java | 6 +- .../spring_boot/RepositoryIT.java | 5 -- .../reactive/ReactiveRepositoryIT.java | 28 +++++++++ .../neo4j/integration/shared/AltHobby.java | 59 +++++++++++++++++++ .../shared/AltLikedByPersonRelationship.java | 35 +++++++++++ .../neo4j/integration/shared/AltPerson.java | 47 +++++++++++++++ 7 files changed, 173 insertions(+), 9 deletions(-) create mode 100644 src/test/java/org/springframework/data/neo4j/integration/shared/AltHobby.java create mode 100644 src/test/java/org/springframework/data/neo4j/integration/shared/AltLikedByPersonRelationship.java create mode 100644 src/test/java/org/springframework/data/neo4j/integration/shared/AltPerson.java diff --git a/pom.xml b/pom.xml index fce4de806..fc9a87a44 100644 --- a/pom.xml +++ b/pom.xml @@ -69,8 +69,6 @@ - ja - 1.1.0 2.0.1 1.6.0 diff --git a/src/main/java/org/springframework/data/neo4j/core/mapping/DefaultNeo4jConverter.java b/src/main/java/org/springframework/data/neo4j/core/mapping/DefaultNeo4jConverter.java index 7fbba1bcc..8a0ba03cc 100644 --- a/src/main/java/org/springframework/data/neo4j/core/mapping/DefaultNeo4jConverter.java +++ b/src/main/java/org/springframework/data/neo4j/core/mapping/DefaultNeo4jConverter.java @@ -282,7 +282,7 @@ final class DefaultNeo4jConverter implements Neo4jConverter { concreteNodeDescription.doWithAssociations( populateFrom(queryResult, propertyAccessor, isConstructorParameter, relationships, knownObjects)); } - return instance; + return propertyAccessor.getBean(); } /** @@ -429,11 +429,13 @@ final class DefaultNeo4jConverter implements Neo4jConverter { return Optional.empty(); } + Function targetIdSelector = relationshipDescription.isOutgoing() ? Relationship::endNodeId : Relationship::startNodeId; + for (Node possibleValueNode : allNodesWithMatchingLabelInResult) { long nodeId = possibleValueNode.id(); for (Relationship possibleRelationship : allMatchingTypeRelationshipsInResult) { - if (possibleRelationship.endNodeId() == nodeId) { + if (targetIdSelector.apply(possibleRelationship) == nodeId) { Object mappedObject = map(possibleValueNode, concreteTargetNodeDescription, knownObjects); if (relationshipDescription.hasRelationshipProperties()) { diff --git a/src/test/java/org/springframework/data/neo4j/documentation/spring_boot/RepositoryIT.java b/src/test/java/org/springframework/data/neo4j/documentation/spring_boot/RepositoryIT.java index 9e0a88b24..c0bf8b60f 100644 --- a/src/test/java/org/springframework/data/neo4j/documentation/spring_boot/RepositoryIT.java +++ b/src/test/java/org/springframework/data/neo4j/documentation/spring_boot/RepositoryIT.java @@ -43,11 +43,6 @@ import org.testcontainers.containers.Neo4jContainer; import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; -// end::testing.reactivedataneo4jtest[] -// tag::testing.reactivedataneo4jtest[] -// end::testing.reactivedataneo4jtest[] -// tag::testing.reactivedataneo4jtest[] - // end::testing.reactivedataneo4jtest[] /** diff --git a/src/test/java/org/springframework/data/neo4j/integration/reactive/ReactiveRepositoryIT.java b/src/test/java/org/springframework/data/neo4j/integration/reactive/ReactiveRepositoryIT.java index 27b5e3637..17609bc29 100644 --- a/src/test/java/org/springframework/data/neo4j/integration/reactive/ReactiveRepositoryIT.java +++ b/src/test/java/org/springframework/data/neo4j/integration/reactive/ReactiveRepositoryIT.java @@ -994,6 +994,27 @@ class ReactiveRepositoryIT { }).verifyComplete(); } + + @Test // DATAGRAPH-1350 + void loadEntityWithRelationshipWithPropertiesFromCustomQueryIncoming( + @Autowired ReactiveHobbyithRelationshipWithPropertiesRepository repository) { + + long personId; + + try (Session session = createSession()) { + Record record = session.run("CREATE (n:AltPerson{name:'Freddie'}), (n)-[l1:LIKES {rating: 5}]->(h1:AltHobby{name:'Music'}) RETURN n, h1").single(); + personId = record.get("n").asNode().id(); + } + + StepVerifier.create(repository.loadFromCustomQuery(personId)).assertNext(hobby -> { + assertThat(hobby.getName()).isEqualTo("Music"); + assertThat(hobby.getLikedBy()).hasSize(1); + assertThat(hobby.getLikedBy().entrySet()).first().satisfies(entry -> { + assertThat(entry.getKey().getId()).isEqualTo(personId); + assertThat(entry.getValue().getRating()).isEqualTo(5); + }); + }).verifyComplete(); + } } @Nested @@ -2053,6 +2074,13 @@ class ReactiveRepositoryIT { Mono findByHobbiesSinceAndHobbiesActive(int since1, boolean active); } + interface ReactiveHobbyithRelationshipWithPropertiesRepository + extends ReactiveNeo4jRepository { + + @Query("MATCH (p:AltPerson)-[l:LIKES]->(h:AltHobby) WHERE id(p) = $personId RETURN h, collect(l), collect(p)") + Flux loadFromCustomQuery(@Param("personId") Long personId); + } + interface ReactivePetRepository extends ReactiveNeo4jRepository {} interface ReactiveRelationshipRepository extends ReactiveNeo4jRepository { diff --git a/src/test/java/org/springframework/data/neo4j/integration/shared/AltHobby.java b/src/test/java/org/springframework/data/neo4j/integration/shared/AltHobby.java new file mode 100644 index 000000000..842c025f0 --- /dev/null +++ b/src/test/java/org/springframework/data/neo4j/integration/shared/AltHobby.java @@ -0,0 +1,59 @@ +/* + * Copyright 2011-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.neo4j.integration.shared; + +import static org.springframework.data.neo4j.core.schema.Relationship.Direction.*; + +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; + +/** + * @@author Michael J. Simons + */ +@Node +public class AltHobby { + @Id @GeneratedValue private Long id; + + private String name; + + @Relationship(type = "LIKES", direction = INCOMING) + private Map likedBy = new HashMap<>(); + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Map getLikedBy() { + return likedBy; + } +} diff --git a/src/test/java/org/springframework/data/neo4j/integration/shared/AltLikedByPersonRelationship.java b/src/test/java/org/springframework/data/neo4j/integration/shared/AltLikedByPersonRelationship.java new file mode 100644 index 000000000..860f59724 --- /dev/null +++ b/src/test/java/org/springframework/data/neo4j/integration/shared/AltLikedByPersonRelationship.java @@ -0,0 +1,35 @@ +/* + * Copyright 2011-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.neo4j.integration.shared; + +import org.springframework.data.neo4j.core.schema.RelationshipProperties; + +/** + * @@author Michael J. Simons + */ +@RelationshipProperties +public class AltLikedByPersonRelationship { + + private Integer rating; + + public Integer getRating() { + return rating; + } + + public void setRating(Integer rating) { + this.rating = rating; + } +} diff --git a/src/test/java/org/springframework/data/neo4j/integration/shared/AltPerson.java b/src/test/java/org/springframework/data/neo4j/integration/shared/AltPerson.java new file mode 100644 index 000000000..f88315388 --- /dev/null +++ b/src/test/java/org/springframework/data/neo4j/integration/shared/AltPerson.java @@ -0,0 +1,47 @@ +/* + * Copyright 2011-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.neo4j.integration.shared; + +import org.springframework.data.neo4j.core.schema.GeneratedValue; +import org.springframework.data.neo4j.core.schema.Id; +import org.springframework.data.neo4j.core.schema.Node; + +/** + * @@author Michael J. Simons + */ +@Node +public class AltPerson { + + @Id @GeneratedValue private Long id; + + private final String name; + + public AltPerson(String name) { + this.name = name; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } +}