Skip to content
This repository has been archived by the owner on May 3, 2021. It is now read-only.

Commit

Permalink
DATAGRAPH-1350 - Incoming relationships are not treated correctly whe…
Browse files Browse the repository at this point in the history
…n using custom queries. (Backport)

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.
  • Loading branch information
michael-simons committed Aug 4, 2020
1 parent c319fe3 commit 4f58375
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ private <ET> ET map(MapAccessor queryResult,
concreteNodeDescription.doWithAssociations(
populateFrom(queryResult, propertyAccessor, isConstructorParameter, relationships, knownObjects));
}
return instance;
return propertyAccessor.getBean();
}

/**
Expand Down Expand Up @@ -459,11 +459,13 @@ private Optional<Object> createInstanceOfRelationships(Neo4jPersistentProperty p
return Optional.empty();
}

Function<Relationship, Long> 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()) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,27 @@ void loadEntityWithRelationshipWithPropertiesFromCustomQuery(@Autowired Reactive
.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
Expand Down Expand Up @@ -2356,6 +2377,13 @@ interface ReactivePersonWithRelationshipWithPropertiesRepository
interface ReactivePetRepository extends ReactiveNeo4jRepository<Pet, Long> {
}

interface ReactiveHobbyithRelationshipWithPropertiesRepository
extends ReactiveNeo4jRepository<AltHobby, Long> {

@Query("MATCH (p:AltPerson)-[l:LIKES]->(h:AltHobby) WHERE id(p) = $personId RETURN h, collect(l), collect(p)")
Flux<AltHobby> loadFromCustomQuery(@Param("personId") Long personId);
}

interface ReactiveRelationshipRepository extends ReactiveNeo4jRepository<PersonWithRelationship, Long> {

@Query("MATCH (n:PersonWithRelationship{name:'Freddie'}) "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2019-2020 "Neo4j,"
* Neo4j Sweden AB [https://neo4j.com]
*
* This file is part of Neo4j.
*
* 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.neo4j.springframework.data.integration.shared;

import java.util.HashMap;
import java.util.Map;

import org.neo4j.springframework.data.core.schema.GeneratedValue;
import org.neo4j.springframework.data.core.schema.Id;
import org.neo4j.springframework.data.core.schema.Node;
import org.neo4j.springframework.data.core.schema.Relationship;
import org.neo4j.springframework.data.core.schema.Relationship.Direction;

/**
* @@author Michael J. Simons
*/
@Node
public class AltHobby {
@Id @GeneratedValue private Long id;

private String name;

@Relationship(type = "LIKES", direction = Direction.INCOMING)
private Map<AltPerson, AltLikedByPersonRelationship> 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<AltPerson, AltLikedByPersonRelationship> getLikedBy() {
return likedBy;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2019-2020 "Neo4j,"
* Neo4j Sweden AB [https://neo4j.com]
*
* This file is part of Neo4j.
*
* 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.neo4j.springframework.data.integration.shared;

import org.neo4j.springframework.data.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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2019-2020 "Neo4j,"
* Neo4j Sweden AB [https://neo4j.com]
*
* This file is part of Neo4j.
*
* 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.neo4j.springframework.data.integration.shared;

import org.neo4j.springframework.data.core.schema.GeneratedValue;
import org.neo4j.springframework.data.core.schema.Id;
import org.neo4j.springframework.data.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;
}
}

0 comments on commit 4f58375

Please sign in to comment.