diff --git a/src/main/java/org/springframework/datastore/graph/neo4j/spi/node/Neo4jNodeBacking.aj b/src/main/java/org/springframework/datastore/graph/neo4j/spi/node/Neo4jNodeBacking.aj index 5b64013fd..28bb6102a 100644 --- a/src/main/java/org/springframework/datastore/graph/neo4j/spi/node/Neo4jNodeBacking.aj +++ b/src/main/java/org/springframework/datastore/graph/neo4j/spi/node/Neo4jNodeBacking.aj @@ -154,6 +154,10 @@ public aspect Neo4jNodeBacking extends AbstractTypeAnnotatingMixinFields relationshipType, String type) { Relationship rel = this.getUnderlyingNode().createRelationshipTo(node.getUnderlyingNode(), DynamicRelationshipType.withName(type)); + return createRelationshipEntity(relationshipType,rel); + } + + private static RelationshipBacked createRelationshipEntity(Class relationshipType, Relationship rel) { try { final RelationshipBacked relationshipEntity = relationshipType.newInstance(); relationshipEntity.setUnderlyingRelationship(rel); @@ -165,6 +169,15 @@ public aspect Neo4jNodeBacking extends AbstractTypeAnnotatingMixinFields relationshipType, String type) { + Node myNode=this.getUnderlyingNode(); + Node otherNode=node.getUnderlyingNode(); + for (Relationship rel : this.getUnderlyingNode().getRelationships(DynamicRelationshipType.withName(type))) { + if (rel.getOtherNode(myNode).equals(otherNode)) return createRelationshipEntity(relationshipType,rel); + } + return null; + } + private Iterable> NodeBacked.eachDirty() { return this.dirty!=null ? this.dirty.entrySet() : Collections.emptyMap().entrySet(); } diff --git a/src/test/java/org/springframework/datastore/graph/neo4j/spi/Neo4jGraphPersistenceTest.java b/src/test/java/org/springframework/datastore/graph/neo4j/spi/Neo4jGraphPersistenceTest.java index 990646e27..1d816e2ef 100644 --- a/src/test/java/org/springframework/datastore/graph/neo4j/spi/Neo4jGraphPersistenceTest.java +++ b/src/test/java/org/springframework/datastore/graph/neo4j/spi/Neo4jGraphPersistenceTest.java @@ -355,6 +355,14 @@ public class Neo4jGraphPersistenceTest { Assert.assertEquals(p, f.getPerson1()); Assert.assertEquals(p2, f.getPerson2()); } + @Test + @Transactional + public void testGetRelationshipToReturnsRelationship() { + Person p = new Person("Michael", 35); + Person p2 = new Person("David", 25); + Friendship f = p.knows(p2); + Assert.assertEquals(f,p.getRelationshipTo(p2,Friendship.class, "knows")); + } @Test @Transactional