Interface Node
- All Superinterfaces:
Entity
relationships
, nodes are the core building
blocks of the Neo4j data representation model. Nodes are created by invoking
the Transaction.createNode()
method.
Node has three major groups of operations: operations that deal with
relationships, operations that deal with properties (see
Entity
) and operations that deal with labels
.
The relationship operations provide a number of overloaded accessors (such as
getRelationships(...)
with "filters" for type, direction, etc),
as well as the factory method createRelationshipTo(...)
that connects two nodes with a relationship. It
also includes the convenience method getSingleRelationship(...)
for accessing the commonly occurring
one-to-zero-or-one association.
The property operations give access to the key-value property pairs. Property
keys are always strings. Valid property value types are all the Java
primitives (int
, byte
, float
, etc),
java.lang.String
s and arrays of primitives and Strings.
Please note that Neo4j does NOT accept arbitrary objects as property
values. setProperty()
takes a
java.lang.Object
only to avoid an explosion of overloaded
setProperty()
methods. For further documentation see
Entity
.
A node'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 TypeMethodDescriptionvoid
Adds aLabel
to this node.createRelationshipTo
(Node otherNode, RelationshipType type) Creates a relationship between this node and another node.void
delete()
Deletes this entity.int
Returns the number of relationships connected to this node regardless of direction or type.int
Returns the number of relationships of a givendirection
connected to this node.int
getDegree
(RelationshipType type) Returns the number of relationships of a giventype
connected to this node.int
getDegree
(RelationshipType type, Direction direction) Returns the number of relationships of a giventype
anddirection
connected to this node.Lists all labels attached to this node.Returns all the relationships attached to this node.getRelationships
(Direction direction, RelationshipType... types) Returns all the relationships of any of the types intypes
that are attached to this node and have the givendirection
.getRelationships
(RelationshipType... types) Returns all the relationships of any of the types intypes
that are attached to this node, regardless of direction.Returns relationship types which this node has one more relationships for.getSingleRelationship
(RelationshipType type, Direction dir) Returns the only relationship of a given type and direction that is attached to this node, ornull
.boolean
Checks whether or not this node has the given label.boolean
Returnstrue
if there are any relationships attached to this node,false
otherwise.boolean
hasRelationship
(Direction dir) Returnstrue
if there are any relationships in the given direction attached to this node,false
otherwise.boolean
hasRelationship
(Direction direction, RelationshipType... types) Returnstrue
if there are any relationships of any of the types intypes
attached to this node (for the givendirection
),false
otherwise.boolean
hasRelationship
(RelationshipType... types) Returnstrue
if there are any relationships of any of the types intypes
attached to this node (regardless of direction),false
otherwise.void
removeLabel
(Label label) Removes aLabel
from this node.Methods inherited from interface org.neo4j.graphdb.Entity
getAllProperties, getElementId, getId, getProperties, getProperty, getProperty, getPropertyKeys, hasProperty, removeProperty, setProperty
-
Method Details
-
delete
void delete()Deletes this entity. All attached relationships must have been deleted before deleting this node.- Specified by:
delete
in interfaceEntity
- Throws:
ConstraintViolationException
- if this node still has relationships attachedNotFoundException
- if any methods are invoked on this entity afterdelete()
-
getRelationships
ResourceIterable<Relationship> getRelationships()Returns all the relationships attached to this node. If no relationships are attached to this node, an empty iterable will be returned.- Returns:
- all relationships attached to this node
-
hasRelationship
boolean hasRelationship()Returnstrue
if there are any relationships attached to this node,false
otherwise.- Returns:
true
if there are any relationships attached to this node,false
otherwise
-
getRelationships
Returns all the relationships of any of the types intypes
that are attached to this node, regardless of direction. If no relationships of the given types are attached to this node, an empty iterable will be returned.- Parameters:
types
- the given relationship type(s)- Returns:
- all relationships of the given type(s) that are attached to this node
-
getRelationships
Returns all the relationships of any of the types intypes
that are attached to this node and have the givendirection
. If no relationships of the given types are attached to this node, an empty iterable will be returned.- Parameters:
types
- the given relationship type(s)direction
- the direction of the relationships to return.- Returns:
- all relationships of the given type(s) that are attached to this node
-
hasRelationship
Returnstrue
if there are any relationships of any of the types intypes
attached to this node (regardless of direction),false
otherwise.- Parameters:
types
- the given relationship type(s)- Returns:
true
if there are any relationships of any of the types intypes
attached to this node,false
otherwise
-
hasRelationship
Returnstrue
if there are any relationships of any of the types intypes
attached to this node (for the givendirection
),false
otherwise.- Parameters:
types
- the given relationship type(s)direction
- the direction to check relationships for- Returns:
true
if there are any relationships of any of the types intypes
attached to this node,false
otherwise
-
getRelationships
Returns allOUTGOING
orINCOMING
relationships from this node. If there are no relationships with the given direction attached to this node, an empty iterable will be returned. IfBOTH
is passed in as a direction, relationships of both directions are returned (effectively turning this intogetRelationships()
).- Parameters:
dir
- the given direction, whereDirection.OUTGOING
means all relationships that have this node asstart node
andDirection.INCOMING
means all relationships that have this node asend node
- Returns:
- all relationships with the given direction that are attached to this node
-
hasRelationship
Returnstrue
if there are any relationships in the given direction attached to this node,false
otherwise. IfBOTH
is passed in as a direction, relationships of both directions are matched (effectively turning this intohasRelationships()
).- Parameters:
dir
- the given direction, whereDirection.OUTGOING
means all relationships that have this node asstart node
andDirection.INCOMING
means all relationships that have this node asend node
- Returns:
true
if there are any relationships in the given direction attached to this node,false
otherwise
-
getSingleRelationship
Returns the only relationship of a given type and direction that is attached to this node, ornull
. This is a convenience method that is used in the commonly occurring situation where a node has exactly zero or one relationships of a given type and direction to another node. Typically this invariant is maintained by the rest of the code: if at any time more than one such relationships exist, it is a fatal error that should generate an unchecked exception. This method reflects that semantics and returns either:null
if there are zero relationships of the given type and direction,- the relationship if there's exactly one, or
- throws an unchecked exception in all other cases.
This method should be used only in situations with an invariant as described above. In those situations, a "state-checking" method (e.g.
hasSingleRelationship(...)
) is not required, because this method behaves correctly "out of the box."- Parameters:
type
- the type of the wanted relationshipdir
- the direction of the wanted relationship (whereDirection.OUTGOING
means a relationship that has this node asstart node
andDirection.INCOMING
means a relationship that has this node asend node
) orDirection.BOTH
if direction is irrelevant- Returns:
- the single relationship matching the given type and direction if
exactly one such relationship exists, or
null
if exactly zero such relationships exists - Throws:
RuntimeException
- if more than one relationship matches the given type and direction
-
createRelationshipTo
Creates a relationship between this node and another node. The relationship is of typetype
. It starts at this node and ends atotherNode
.A relationship is equally well traversed in both directions so there's no need to create another relationship in the opposite direction (in regards to traversal or performance).
- Parameters:
otherNode
- the end node of the new relationshiptype
- the type of the new relationship- Returns:
- the newly created relationship
-
getRelationshipTypes
Iterable<RelationshipType> getRelationshipTypes()Returns relationship types which this node has one more relationships for. If this node doesn't have any relationships an emptyIterable
will be returned.- Returns:
- relationship types which this node has one more relationships for.
-
getDegree
int getDegree()Returns the number of relationships connected to this node regardless of direction or type. This operation is always O(1).- Returns:
- the number of relationships connected to this node.
-
getDegree
Returns the number of relationships of a giventype
connected to this node.- Parameters:
type
- the type of relationships to get the degree for- Returns:
- the number of relationships of a given
type
connected to this node.
-
getDegree
Returns the number of relationships of a givendirection
connected to this node.- Parameters:
direction
- the direction of the relationships- Returns:
- the number of relationships of a given
direction
for this node.
-
getDegree
Returns the number of relationships of a giventype
anddirection
connected to this node.- Parameters:
type
- the type of relationships to get the degree fordirection
- the direction of the relationships- Returns:
- the number of relationships of a given
type
anddirection
for this node.
-
addLabel
Adds aLabel
to this node. If this node doesn't already have this label it will be added. If it already has the label, nothing will happen.- Parameters:
label
- the label to add to this node.
-
removeLabel
Removes aLabel
from this node. If this node doesn't have this label, nothing will happen.- Parameters:
label
- the label to remove from this node.
-
hasLabel
Checks whether or not this node has the given label.- Parameters:
label
- the label to check for.- Returns:
true
if this node has the given label, otherwisefalse
.
-
getLabels
Lists all labels attached to this node. If this node has no labels an emptyIterable
will be returned.- Returns:
- all labels attached to this node.
-