Interface Relationship
- All Superinterfaces:
Entity
type
. You can attach
properties to relationships with the API specified in
Entity
.
Relationships are created by invoking the
Node.createRelationshipTo()
method on a node as follows:
Relationship rel = node.
createRelationshipTo
( otherNode, MyRels.REL_TYPE );
The fact that the relationship API gives meaning to start
and end
nodes implicitly means that all
relationships have a direction. In the example above, rel
would
be directed from node
to otherNode
. A
relationship's start node and end node and their relation to
Direction.OUTGOING
and Direction.INCOMING
are defined so that
the assertions in the following code are true
:
Node
a = tx.createNode
();
Node
b = tx.createNode
();
Relationship
rel = a.createRelationshipTo
( b, MyRels.REL_TYPE
);
// Now we have: (a) --- REL_TYPE ---> (b)
assert rel.getStartNode
().equals( a );
assert rel.getEndNode
().equals( b );
assert rel.getNodes
()[0].equals( a ) &&
rel.getNodes
()[1].equals( b );
Even though all relationships have a direction they are equally well
traversed in both directions so there's no need to create duplicate
relationships in the opposite direction (with regard to traversal or
performance).
Furthermore, Neo4j guarantees that a relationship is never "hanging freely,"
i.e. getStartNode()
, getEndNode()
,
getOtherNode(Node)
and getNodes()
are guaranteed to always
return valid, non-null nodes.
A relationship's id is unique, but note the following: Neo4j reuses its internal ids when nodes and relationships are deleted, which means it's bad practice to refer to them this way. Instead, use application generated ids.
-
Method Summary
Modifier and TypeMethodDescriptionReturns the end node of this relationship.default long
Deprecated, for removal: This API element is subject to removal in a future version.Node[]
getNodes()
Returns the two nodes that are attached to this relationship.getOtherNode
(Node node) A convenience operation that, given a node that is attached to this relationship, returns the other node.default long
getOtherNodeId
(long id) Deprecated, for removal: This API element is subject to removal in a future version.Returns the start node of this relationship.default long
Deprecated, for removal: This API element is subject to removal in a future version.getType()
Returns the type of this relationship.boolean
isType
(RelationshipType type) Indicates whether this relationship is of the typetype
.Methods inherited from interface org.neo4j.graphdb.Entity
delete, getAllProperties, getElementId, getId, getProperties, getProperty, getProperty, getPropertyKeys, hasProperty, removeProperty, setProperty
-
Method Details
-
getStartNode
Node getStartNode()Returns the start node of this relationship. For a definition of how start node relates todirections
as arguments to therelationship accessors
in Node, see the class documentation of Relationship.- Returns:
- the start node of this relationship
-
getEndNode
Node getEndNode()Returns the end node of this relationship. For a definition of how end node relates todirections
as arguments to therelationship accessors
in Node, see the class documentation of Relationship.- Returns:
- the end node of this relationship
-
getOtherNode
A convenience operation that, given a node that is attached to this relationship, returns the other node. For example ifnode
is a start node, the end node will be returned, and vice versa. This is a very convenient operation when you're manually traversing the graph by invoking one of thegetRelationships()
operations on a node. For example, to get the node "at the other end" of a relationship, use the following:Node endNode = node.getSingleRelationship( MyRels.REL_TYPE ).getOtherNode( node );
This operation will throw a runtime exception if
node
is neither this relationship's start node nor its end node.- Parameters:
node
- the start or end node of this relationship- Returns:
- the other node
- Throws:
RuntimeException
- if the given node is neither the start nor end node of this relationship
-
getNodes
Node[] getNodes()Returns the two nodes that are attached to this relationship. The first element in the array will be the start node, the second element the end node.- Returns:
- the two nodes that are attached to this relationship
-
getType
RelationshipType getType()Returns the type of this relationship. A relationship's type is an immutable attribute that is specified at Relationshipcreation
. Remember that relationship types are semantically equivalent if theirnames
areequal
. This is NOT the same as checking for identity equality using the == operator. If you want to know whether this relationship is of a certain type, use theisType()
operation.- Returns:
- the type of this relationship
-
isType
Indicates whether this relationship is of the typetype
. This is a convenience method that checks for equality using the contract specified by RelationshipType, i.e. by checking for equalnames
.- Parameters:
type
- the type to check- Returns:
true
if this relationship is of the typetype
,false
otherwise or ifnull
-
getStartNodeId
Deprecated, for removal: This API element is subject to removal in a future version.Returns the id of the start node of this relationship. For a definition of how start node relates todirections
as arguments to therelationship accessors
in Node, see the class documentation of Relationship.Note that this id can get reused in the future, if this relationship and the given node are deleted.
- Returns:
- the id of the start node of this relationship.
-
getEndNodeId
Deprecated, for removal: This API element is subject to removal in a future version.Returns the id of the end node of this relationship. For a definition of how end node relates todirections
as arguments to therelationship accessors
in Node, see the class documentation of Relationship.Note that this id can get reused in the future, if this relationship and the given node are deleted.
- Returns:
- the id of the end node of this relationship.
-
getOtherNodeId
Deprecated, for removal: This API element is subject to removal in a future version.A convenience operation that, given an id of a node that is attached to this relationship, returns the id of the other node. For example ifid
is the start node id, the end node id will be returned, and vice versa.This operation will throw a runtime exception if
id
is not the id of either of this relationship's nodes.Note that this id can get reused in the future, if this relationship and the given node are deleted.
- Parameters:
id
- the id of the start or end node of this relationship- Returns:
- the id of the other node
- Throws:
RuntimeException
- if the given node id is not the id of either the start or end node of this relationship.
-