Database Reference
In-Depth Information
fromthe
user
nodeandbedirectedtothe
movie
node,thesameasthe
IS_FRIEND_OF
relationship you used before.
Thereisonesignificantdifferencebetweenthe
HAS_SEEN
and
IS_FRIEND_OF
relation-
ships:the
HAS_SEEN
relationshipcontainstheadditionalinformationabouthowmuchthe
user liked the movie (represented by the number of stars the user gave to the movie). The
number of stars is therefore not a property of the
user
node (because a user can rate mul-
tiplemovies)orofthe
movie
node(themoviecanberatedbymultiple users),butrathera
property of the relationship between the user and the movie. You've seen how you can add
properties to nodes, but adding properties to relationships is a new concept at this point.
We mentioned before that Neo4j is a property graph. In Neo4j,
property graph
applies to
all graph entities—relationships as well as nodes. Adding a property to a relationship in
Neo4jisassimpleasaddingapropertytoanode.We'llguideyouthroughusingtheNeo4j
Core Java API to add properties to relationships.
Before we proceed, you need to add a new relationship type to the relationship enum.
The following snippet shows the updated
MyRelationshipTypes
enum with the new
HAS_SEEN
relationship:
public enum MyRelationshipTypes implements RelationshipType{
IS_FRIEND_OF,
HAS_SEEN;
}
The next step is to create named relationships between nodes, which is achieved in the
same way that you added friendship relationships between users earlier in the chapter.
The API call for the creation of a relationship will return the Neo4j
Relationship
ob-
ject, and you can use it to add properties by calling the
Relationship.set-Prop-
erty(String name, Object value)
method.
The following listing shows the code that creates the
HAS_SEEN
relationships and adds
stars
properties to them.