Database Reference
In-Depth Information
Both the start and end nodes of a relationship must already be established within the database before the
relationship can be saved.
Note
Retrieving Relationships
Once a relationship has been created between one or more nodes, the relationship can be retrieved based on the
nodes relating to it. If no relationship exists, the relationship will be set to null (Listing 11-8).
Listing 11-8. Retrieving Relationships
public class SDNServiceClass {
@Autowired
public Neo4jOperations neo4jTemplate;
public void retrieveRelationship(){
// retrieve the node by its node id value, in this case 10
Node greg = neo4jTemplate.getNode(10);
// retrieve the node by its node id value, in this case 1
Node jeremy = neo4jTemplate.getNode(1);
// retrieve the relationship (greg follows jeremy)
Relationship rel = neo4jTemplate.getRelationshipBetween(greg,jeremy,"FOLLOWS");
}
}
Deleting a Relationship
Once a relationship's graph id has been set and saved into the database, it becomes eligible to be removed when
necessary. To remove a relationship, set it as a relationship object instance and then call the delete method for the
relationship (Listing 11-9).
Listing 11-9. Deleting a Relationship
public class SDNServiceClass {
@Autowired
public Neo4jOperations neo4jTemplate;
public void deleteRelationship(){
// retrieve the Relationship by its Relationship id value, in this case 20
Relationship relationship = neo4jTemplate.getRelationship(20);
 
 
Search WWH ::




Custom Search