Skip to content

Commit 89f3f08

Browse files
committed
DATAGRAPH-1434 - Fix naming of relationships in query.
1 parent c1cb92f commit 89f3f08

File tree

4 files changed

+126
-3
lines changed

4 files changed

+126
-3
lines changed

src/main/java/org/springframework/data/neo4j/core/mapping/CypherGenerator.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,10 @@ private void generateListFor(RelationshipDescription relationshipDescription, Sy
566566

567567
String relationshipType = relationshipDescription.getType();
568568
String relationshipTargetName = relationshipDescription.generateRelatedNodesCollectionName();
569+
String sourcePrimaryLabel = relationshipDescription.getSource().getPrimaryLabel();
569570
String targetPrimaryLabel = relationshipDescription.getTarget().getPrimaryLabel();
570571
List<String> targetAdditionalLabels = relationshipDescription.getTarget().getAdditionalLabels();
572+
String relationshipSymbolicName = sourcePrimaryLabel + RelationshipDescription.NAME_OF_RELATIONSHIP + targetPrimaryLabel;
571573

572574
Node startNode = anyNode(nodeName);
573575
SymbolicName relationshipFieldName = nodeName.concat("_" + fieldName);
@@ -586,7 +588,7 @@ private void generateListFor(RelationshipDescription relationshipDescription, Sy
586588
new ArrayList<>(processedRelationships));
587589

588590
if (relationshipDescription.hasRelationshipProperties()) {
589-
relationship = relationship.named(RelationshipDescription.NAME_OF_RELATIONSHIP);
591+
relationship = relationship.named(relationshipSymbolicName);
590592
mapProjection = mapProjection.and(relationship);
591593
}
592594

@@ -604,7 +606,7 @@ private void generateListFor(RelationshipDescription relationshipDescription, Sy
604606
new ArrayList<>(processedRelationships));
605607

606608
if (relationshipDescription.hasRelationshipProperties()) {
607-
relationship = relationship.named(RelationshipDescription.NAME_OF_RELATIONSHIP);
609+
relationship = relationship.named(relationshipSymbolicName);
608610
mapProjection = mapProjection.and(relationship);
609611
}
610612

src/main/java/org/springframework/data/neo4j/core/mapping/DefaultNeo4jEntityConverter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ private Optional<Object> createInstanceOfRelationships(Neo4jPersistentProperty p
356356
.filter(r -> r.getFieldName().equals(persistentProperty.getName())).findFirst().get();
357357

358358
String relationshipType = relationshipDescription.getType();
359+
String sourceLabel = relationshipDescription.getSource().getPrimaryLabel();
359360
String targetLabel = relationshipDescription.getTarget().getPrimaryLabel();
360361

361362
Neo4jPersistentEntity<?> genericTargetNodeDescription = (Neo4jPersistentEntity<?>) relationshipDescription
@@ -491,7 +492,9 @@ private Optional<Object> createInstanceOfRelationships(Neo4jPersistentProperty p
491492
Object valueEntry = map(relatedEntity, allValues, concreteTargetNodeDescription, knownObjects, processedSegments);
492493

493494
if (relationshipDescription.hasRelationshipProperties()) {
494-
Relationship relatedEntityRelationship = relatedEntity.get(RelationshipDescription.NAME_OF_RELATIONSHIP)
495+
String relationshipSymbolicName = sourceLabel
496+
+ RelationshipDescription.NAME_OF_RELATIONSHIP + targetLabel;
497+
Relationship relatedEntityRelationship = relatedEntity.get(relationshipSymbolicName)
495498
.asRelationship();
496499

497500
Object relationshipProperties = map(relatedEntityRelationship, allValues,

src/test/java/org/springframework/data/neo4j/integration/imperative/RepositoryIT.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
import org.springframework.data.neo4j.integration.shared.common.DtoPersonProjection;
9292
import org.springframework.data.neo4j.integration.shared.common.DtoPersonProjectionContainingAdditionalFields;
9393
import org.springframework.data.neo4j.integration.shared.common.EntityWithConvertedId;
94+
import org.springframework.data.neo4j.integration.shared.common.EntityWithRelationshipPropertiesPath;
9495
import org.springframework.data.neo4j.integration.shared.common.ExtendedParentNode;
9596
import org.springframework.data.neo4j.integration.shared.common.Friend;
9697
import org.springframework.data.neo4j.integration.shared.common.FriendshipRelationship;
@@ -1392,6 +1393,27 @@ void loadSameNodeWithDoubleRelationship(@Autowired HobbyWithRelationshipWithProp
13921393

13931394
assertThat(likedBy).containsExactlyInAnyOrder(rel1, rel2);
13941395
}
1396+
1397+
@Test // DATAGRAPH-1434
1398+
void findAndMapMultipleLevelRelationshipProperties(
1399+
@Autowired EntityWithRelationshipPropertiesPathRepository repository) {
1400+
1401+
long eId;
1402+
1403+
try (Session session = createSession()) {
1404+
eId = session.run("CREATE (n:EntityWithRelationshipPropertiesPath)-[:RelationshipA]->(:EntityA)" +
1405+
"-[:RelationshipB]->(:EntityB)" +
1406+
" RETURN id(n) as eId").single().get("eId").asLong();
1407+
}
1408+
1409+
EntityWithRelationshipPropertiesPath entity = repository.findById(eId).get();
1410+
assertThat(entity).isNotNull();
1411+
assertThat(entity.getRelationshipA()).isNotNull();
1412+
assertThat(entity.getRelationshipA().getEntityA()).isNotNull();
1413+
assertThat(entity.getRelationshipA().getEntityA().getRelationshipB()).isNotNull();
1414+
assertThat(entity.getRelationshipA().getEntityA().getRelationshipB().getEntityB()).isNotNull();
1415+
}
1416+
13951417
}
13961418

13971419
@Nested
@@ -3475,6 +3497,9 @@ interface SimpleEntityWithRelationshipARepository extends Neo4jRepository<Simple
34753497

34763498
interface ThingWithFixedGeneratedIdRepository extends Neo4jRepository<ThingWithFixedGeneratedId, String> {}
34773499

3500+
interface EntityWithRelationshipPropertiesPathRepository
3501+
extends Neo4jRepository<EntityWithRelationshipPropertiesPath, Long> {}
3502+
34783503
@SpringJUnitConfig(Config.class)
34793504
static abstract class IntegrationTestBase {
34803505

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2011-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.neo4j.integration.shared.common;
17+
18+
import org.springframework.data.neo4j.core.schema.GeneratedValue;
19+
import org.springframework.data.neo4j.core.schema.Id;
20+
import org.springframework.data.neo4j.core.schema.Node;
21+
import org.springframework.data.neo4j.core.schema.Relationship;
22+
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
23+
import org.springframework.data.neo4j.core.schema.TargetNode;
24+
25+
/**
26+
* @author Gerrit Meier
27+
*/
28+
@Node
29+
public class EntityWithRelationshipPropertiesPath {
30+
31+
@Id @GeneratedValue private Long id;
32+
33+
@Relationship("RelationshipA")
34+
private RelationshipPropertyA relationshipA;
35+
36+
public RelationshipPropertyA getRelationshipA() {
37+
return relationshipA;
38+
}
39+
40+
/**
41+
* From EntityWithRelationshipPropertiesPath to EntityA
42+
*/
43+
@RelationshipProperties
44+
public static class RelationshipPropertyA {
45+
46+
@TargetNode
47+
private EntityA entityA;
48+
49+
public EntityA getEntityA() {
50+
return entityA;
51+
}
52+
}
53+
54+
/**
55+
* From EntityA to EntityB
56+
*/
57+
@RelationshipProperties
58+
public static class RelationshipPropertyB {
59+
60+
@TargetNode
61+
private EntityB entityB;
62+
63+
public EntityB getEntityB() {
64+
return entityB;
65+
}
66+
}
67+
68+
/**
69+
* EntityA
70+
*/
71+
@Node
72+
public static class EntityA {
73+
@Id @GeneratedValue private Long id;
74+
75+
@Relationship("RelationshipB")
76+
private RelationshipPropertyB relationshipB;
77+
78+
public RelationshipPropertyB getRelationshipB() {
79+
return relationshipB;
80+
}
81+
}
82+
83+
/**
84+
* EntityB
85+
*/
86+
@Node
87+
public static class EntityB {
88+
@Id @GeneratedValue private Long id;
89+
90+
}
91+
92+
93+
}

0 commit comments

Comments
 (0)