Database Reference
In-Depth Information
Listing 4.3. Finding movies that have been seen by John's friends
The first part of this code stores the friends of John in the local variable
friends
,
,
. First, you iterate through all the
IS_FRIEND_OF
relationships of the starting
node, regardless of their direction . Remember, although the
IS_FRIEND_OF
rela-
tionship has a starting and an ending node, you can go upstream as well as downstream
to locate friends. But because you don't know whether
John
is the starting or ending
node of the relationships, you can't use the
Relationship.getStartNode()
or
Relationship.getEndNode()
methods to determine the other participant in the
friendship. Luckily, the Neo4j Core Java API has a solution for that: the
Relation-
ship.getOtherNode(Node node)
method. This method, given a node on one end
of the relationship, returns the node on the other end. In this case, because you know that
the relationship has one end at the
John
node, you can use this method to find out John's
friends
. Finally, you add the
friend
node to the local
java.lang.Set
variable
friends
, which you'll use later.
In the second part of the code, you find all the movies John's friends have seen
,
,
. For each of the nodes stored in the temporary local variable
, you check all out-
going
HAS_SEEN
relationships
. Just as you did in the previous example, you load the