Database Reference
In-Depth Information
Note
Recall from
chapter 3
that all Neo4j operations (including read operations from Neo4j 2.0
onward) need to be wrapped in a transaction.
Listing 4.1
includes this initial boilerplate
wrapping code, but we'll leave it out of future listings to reduce clutter and focus on the
core aspects of the API under discussion. Don't forget about it, though, as the code won't
work without it!
lationships that either start or end at the selected node . This method returns a
java.lang.Iterable
object that contains instances of
org.neo4j.graphdb.Relationship
,whichyoucaniterate throughusingtheJava
for
loop in the typical Java manner . The
userJohn.getRelationships()
method will return all relationships starting or ending at the starting node, including the
IS_FRIEND_OF
relationships that you're not interested in (see
figure 4.1
). To identify
only the relationships of type
HAS_SEEN
that you're interested in, you need to check the
name of each relationship within the loop
.
To get the nodes that the relationship is associated with, the
org.neo4j.graphdb.Relationship
interface defines two methods:
getStartNode()
, which returns the starting node of the relationship, and
getEndNode()
,whichreturnstheendingnodeoftherelationship.Wementionedearlier
that Neo4j incorporates a directed property graph model for its underlying graph storage.
Every relationship in the Neo4j graph is directed, originating from the starting node to the
ending node. For example, a
HAS_SEEN
relationship always originates from the
user
nodetothe
movie
node.Asaresult,thestartingnodewillalwaysbethenoderepresenting
a user, and the ending node will be the node representing a movie.
ways be an ending node of the
HAS_SEEN
relationship, so you get a reference to it by
calling the
Relationship.getEndNode()
method
.