added support for getting a direct relationship entity

This commit is contained in:
Michael Hunger
2010-09-05 01:53:10 +02:00
parent 695cf93ebc
commit c99dd661cf
2 changed files with 21 additions and 0 deletions

View File

@@ -154,6 +154,10 @@ public aspect Neo4jNodeBacking extends AbstractTypeAnnotatingMixinFields<GraphEn
*/
public RelationshipBacked NodeBacked.relateTo(NodeBacked node, Class<? extends RelationshipBacked> relationshipType, String type) {
Relationship rel = this.getUnderlyingNode().createRelationshipTo(node.getUnderlyingNode(), DynamicRelationshipType.withName(type));
return createRelationshipEntity(relationshipType,rel);
}
private static RelationshipBacked createRelationshipEntity(Class<? extends RelationshipBacked> relationshipType, Relationship rel) {
try {
final RelationshipBacked relationshipEntity = relationshipType.newInstance();
relationshipEntity.setUnderlyingRelationship(rel);
@@ -165,6 +169,15 @@ public aspect Neo4jNodeBacking extends AbstractTypeAnnotatingMixinFields<GraphEn
}
}
public RelationshipBacked NodeBacked.getRelationshipTo(NodeBacked node, Class<? extends RelationshipBacked> 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<Map.Entry<Field,Object>> NodeBacked.eachDirty() {
return this.dirty!=null ? this.dirty.entrySet() : Collections.<Field, Object>emptyMap().entrySet();
}

View File

@@ -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