Database Reference
In-Depth Information
Removing a Node
Once a node's graph id has been set and saved into the database, it becomes eligible to be removed when necessary.
To remove a node, set a variable as a node object instance and then call the delete method for the node (Listing 11-6).
Listing 11-6. Deleting a Node
public class SDNServiceClass {
@Autowired
public Neo4jOperations neo4jTemplate;
public void deleteNode(){
// get the node
Node node = neo4jTemplate.getNode(10);
// delete node
neo4jTemplate.delete(node);
}
}
You cannot delete any node that is currently set as the start point or end point of any relationship. You must
remove the relationship before you can delete the node.
Note
Creating a Relationship
SDN offers a few methods to create relationships, one using the createRelationshipBetween method and another
using the getOrCreateRelationship method. The example in Listing 11-7 sets up the relationship using the
createRelationshipBetween method.
Listing 11-7. Relating Two Nodes
public class SDNServiceClass {
@Autowired
public Neo4jOperations neo4jTemplate;
public void relateNodes(){
// 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);
// populate & save the relationship (greg follows jeremy)
neo4jTemplate.createRelationshipBetween(greg, jeremy, "FOLLOWS", null);
}
}
 
 
Search WWH ::




Custom Search