Neo4j Community Edition - The world's leading Graph Database with Cypher query language and ACID transactions.
—
Property graph model implementation with nodes, relationships, labels, and properties providing the foundational data structures for representing and manipulating connected data with full type support.
Represents a node in the graph with properties and relationships, supporting label management and relationship creation.
/**
* A node in the graph with properties and relationships
*/
public interface Node extends Entity {
/**
* Create a relationship from this node to another node
* @param otherNode Target node for the relationship
* @param type Type of the relationship to create
* @return New relationship instance
*/
Relationship createRelationshipTo(Node otherNode, RelationshipType type);
/**
* Get all relationships connected to this node
* @return Iterable of all relationships
*/
Iterable<Relationship> getRelationships();
/**
* Get relationships of specific types
* @param types Relationship types to filter by
* @return Iterable of matching relationships
*/
Iterable<Relationship> getRelationships(RelationshipType... types);
/**
* Get relationships in a specific direction
* @param direction Direction to filter by (OUTGOING, INCOMING, BOTH)
* @return Iterable of relationships in the specified direction
*/
Iterable<Relationship> getRelationships(Direction direction);
/**
* Get relationships of specific types and direction
* @param direction Direction to filter by
* @param types Relationship types to filter by
* @return Iterable of matching relationships
*/
Iterable<Relationship> getRelationships(Direction direction, RelationshipType... types);
/**
* Get a single relationship of a specific type and direction
* @param type Relationship type
* @param direction Direction
* @return Single relationship, or null if none found
* @throws MoreThanOneRelationshipException if multiple relationships found
*/
Relationship getSingleRelationship(RelationshipType type, Direction direction);
/**
* Check if this node has any relationships
* @return true if node has relationships, false otherwise
*/
boolean hasRelationship();
/**
* Check if this node has relationships of specific types
* @param types Relationship types to check for
* @return true if node has any of the specified relationship types
*/
boolean hasRelationship(RelationshipType... types);
/**
* Check if this node has relationships in a specific direction
* @param direction Direction to check
* @return true if node has relationships in the specified direction
*/
boolean hasRelationship(Direction direction);
/**
* Check if this node has a specific label
* @param label Label to check for
* @return true if node has the label, false otherwise
*/
boolean hasLabel(Label label);
/**
* Get all labels on this node
* @return Iterable of labels
*/
Iterable<Label> getLabels();
/**
* Add a label to this node
* @param label Label to add
*/
void addLabel(Label label);
/**
* Remove a label from this node
* @param label Label to remove
*/
void removeLabel(Label label);
/**
* Get the degree (number of relationships) of this node
* @return Total number of relationships
*/
int getDegree();
/**
* Get the degree for specific relationship types
* @param types Relationship types to count
* @return Number of relationships of the specified types
*/
int getDegree(RelationshipType... types);
/**
* Get the degree in a specific direction
* @param direction Direction to count
* @return Number of relationships in the specified direction
*/
int getDegree(Direction direction);
/**
* Get the degree for specific relationship type and direction
* @param type Relationship type to count
* @param direction Direction to count
* @return Number of relationships of the specified type and direction
*/
int getDegree(RelationshipType type, Direction direction);
/**
* Get a single relationship of the specified type and direction
* @param type Relationship type
* @param direction Direction
* @return Single relationship, or null if none exists
* @throws RuntimeException if more than one relationship exists
*/
Relationship getSingleRelationship(RelationshipType type, Direction direction);
}Usage Examples:
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.RelationshipType;
import static org.neo4j.graphdb.Label.label;
import static org.neo4j.graphdb.RelationshipType.withName;
try (Transaction tx = graphDb.beginTx()) {
// Create nodes with labels and properties
Node alice = tx.createNode(label("Person"), label("Employee"));
alice.setProperty("name", "Alice");
alice.setProperty("age", 30);
alice.setProperty("department", "Engineering");
Node bob = tx.createNode(label("Person"));
bob.setProperty("name", "Bob");
bob.setProperty("age", 25);
// Create relationships
Relationship friendship = alice.createRelationshipTo(bob, withName("FRIENDS"));
friendship.setProperty("since", "2020-01-15");
friendship.setProperty("strength", 0.8);
// Label operations
if (alice.hasLabel(label("Employee"))) {
alice.addLabel(label("Manager"));
}
// Relationship traversal
for (Relationship rel : alice.getRelationships(Direction.OUTGOING)) {
Node other = rel.getEndNode();
System.out.println("Alice is connected to: " + other.getProperty("name"));
}
// Check relationship existence
if (alice.hasRelationship(withName("FRIENDS"))) {
System.out.println("Alice has friends");
}
// Get degree information
System.out.println("Alice has " + alice.getDegree() + " relationships");
tx.commit();
}Represents a relationship between two nodes with properties and type information.
/**
* Relationship between nodes with properties and type
*/
public interface Relationship extends Entity {
/**
* Get the start node of this relationship
* @return Start node
*/
Node getStartNode();
/**
* Get the end node of this relationship
* @return End node
*/
Node getEndNode();
/**
* Get the other node (not the specified node) in this relationship
* @param node One of the nodes in this relationship
* @return The other node
* @throws RuntimeException if the provided node is not part of this relationship
*/
Node getOtherNode(Node node);
/**
* Get the type of this relationship
* @return Relationship type
*/
RelationshipType getType();
/**
* Check if this relationship is of a specific type
* @param type Type to check against
* @return true if relationship is of the specified type
*/
boolean isType(RelationshipType type);
/**
* Delete this relationship
*/
void delete();
}Usage Examples:
try (Transaction tx = graphDb.beginTx()) {
Node alice = tx.findNode(label("Person"), "name", "Alice");
// Traverse relationships
for (Relationship rel : alice.getRelationships()) {
Node other = rel.getOtherNode(alice);
System.out.println("Relationship: " + alice.getProperty("name") +
" -[:" + rel.getType().name() + "]-> " +
other.getProperty("name"));
// Access relationship properties
if (rel.hasProperty("since")) {
System.out.println(" Since: " + rel.getProperty("since"));
}
// Type checking
if (rel.isType(withName("FRIENDS"))) {
System.out.println(" This is a friendship");
}
}
tx.commit();
}Base interface for nodes and relationships providing common property operations.
/**
* Base interface for nodes and relationships
*/
public interface Entity {
/**
* Get the unique ID of this entity (deprecated - use getElementId instead)
* @return Entity ID
* @deprecated Use getElementId() for new code
*/
@Deprecated
long getId();
/**
* Get the element ID of this entity (preferred over getId)
* @return Element ID string
*/
String getElementId();
/**
* Get a property value
* @param key Property key
* @return Property value, or null if not found
*/
Object getProperty(String key);
/**
* Get a property value with a default
* @param key Property key
* @param defaultValue Default value if property not found
* @return Property value or default value
*/
Object getProperty(String key, Object defaultValue);
/**
* Set a property value
* @param key Property key
* @param value Property value
*/
void setProperty(String key, Object value);
/**
* Check if a property exists
* @param key Property key
* @return true if property exists, false otherwise
*/
boolean hasProperty(String key);
/**
* Remove a property
* @param key Property key to remove
* @return Previous value, or null if property didn't exist
*/
Object removeProperty(String key);
/**
* Get all property keys
* @return Iterable of property keys
*/
Iterable<String> getPropertyKeys();
/**
* Get all properties as a map
* @return Map of property key-value pairs
*/
Map<String, Object> getProperties(String... keys);
/**
* Get all properties as a map
* @return Map of all property key-value pairs
*/
Map<String, Object> getAllProperties();
}Interface for node labels providing categorization and indexing capabilities.
/**
* Labels for categorizing nodes
*/
public interface Label {
/**
* Get the name of this label
* @return Label name
*/
String name();
/**
* Create a label with the specified name
* @param name Label name
* @return Label instance
*/
static Label label(String name) {
return org.neo4j.graphdb.Label.label(name);
}
}Interface for relationship types providing categorization and traversal filtering.
/**
* Types for categorizing relationships
*/
public interface RelationshipType {
/**
* Get the name of this relationship type
* @return Type name
*/
String name();
/**
* Create a relationship type with the specified name
* @param name Type name
* @return RelationshipType instance
*/
static RelationshipType withName(String name) {
return org.neo4j.graphdb.RelationshipType.withName(name);
}
}Enum for specifying relationship traversal direction.
/**
* Direction for relationship traversal
*/
public enum Direction {
/** Outgoing relationships (this node is the start node) */
OUTGOING,
/** Incoming relationships (this node is the end node) */
INCOMING,
/** Both outgoing and incoming relationships */
BOTH
}Represents a path through the graph containing nodes and relationships.
/**
* Represents a path through the graph
*/
public interface Path extends Iterable<Entity> {
/**
* Get the start node of this path
* @return Start node
*/
Node startNode();
/**
* Get the end node of this path
* @return End node
*/
Node endNode();
/**
* Get the last relationship in this path
* @return Last relationship, or null if path has no relationships
*/
Relationship lastRelationship();
/**
* Get all nodes in this path
* @return Iterable of nodes
*/
Iterable<Node> nodes();
/**
* Get all relationships in this path
* @return Iterable of relationships
*/
Iterable<Relationship> relationships();
/**
* Get the length of this path (number of relationships)
* @return Path length
*/
int length();
/**
* Reverse this path
* @return New path with reversed direction
*/
Path reverse();
}Usage Examples:
// Working with properties
try (Transaction tx = graphDb.beginTx()) {
Node node = tx.createNode(label("Person"));
// Set various property types
node.setProperty("name", "Alice");
node.setProperty("age", 30);
node.setProperty("active", true);
node.setProperty("scores", new int[]{85, 92, 78});
node.setProperty("metadata", Map.of("department", "Engineering", "level", "Senior"));
// Get properties with type safety
String name = (String) node.getProperty("name");
Integer age = (Integer) node.getProperty("age", 0);
Boolean active = (Boolean) node.getProperty("active");
// Check property existence
if (node.hasProperty("email")) {
String email = (String) node.getProperty("email");
}
// Iterate over all properties
for (String key : node.getPropertyKeys()) {
Object value = node.getProperty(key);
System.out.println(key + " = " + value);
}
// Get properties as map
Map<String, Object> properties = node.getAllProperties();
tx.commit();
}Install with Tessl CLI
npx tessl i tessl/maven-org-neo4j--neo4j