0
# Graph Data Model
1
2
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.
3
4
## Capabilities
5
6
### Node Interface
7
8
Represents a node in the graph with properties and relationships, supporting label management and relationship creation.
9
10
```java { .api }
11
/**
12
* A node in the graph with properties and relationships
13
*/
14
public interface Node extends Entity {
15
16
/**
17
* Create a relationship from this node to another node
18
* @param otherNode Target node for the relationship
19
* @param type Type of the relationship to create
20
* @return New relationship instance
21
*/
22
Relationship createRelationshipTo(Node otherNode, RelationshipType type);
23
24
/**
25
* Get all relationships connected to this node
26
* @return Iterable of all relationships
27
*/
28
Iterable<Relationship> getRelationships();
29
30
/**
31
* Get relationships of specific types
32
* @param types Relationship types to filter by
33
* @return Iterable of matching relationships
34
*/
35
Iterable<Relationship> getRelationships(RelationshipType... types);
36
37
/**
38
* Get relationships in a specific direction
39
* @param direction Direction to filter by (OUTGOING, INCOMING, BOTH)
40
* @return Iterable of relationships in the specified direction
41
*/
42
Iterable<Relationship> getRelationships(Direction direction);
43
44
/**
45
* Get relationships of specific types and direction
46
* @param direction Direction to filter by
47
* @param types Relationship types to filter by
48
* @return Iterable of matching relationships
49
*/
50
Iterable<Relationship> getRelationships(Direction direction, RelationshipType... types);
51
52
/**
53
* Get a single relationship of a specific type and direction
54
* @param type Relationship type
55
* @param direction Direction
56
* @return Single relationship, or null if none found
57
* @throws MoreThanOneRelationshipException if multiple relationships found
58
*/
59
Relationship getSingleRelationship(RelationshipType type, Direction direction);
60
61
/**
62
* Check if this node has any relationships
63
* @return true if node has relationships, false otherwise
64
*/
65
boolean hasRelationship();
66
67
/**
68
* Check if this node has relationships of specific types
69
* @param types Relationship types to check for
70
* @return true if node has any of the specified relationship types
71
*/
72
boolean hasRelationship(RelationshipType... types);
73
74
/**
75
* Check if this node has relationships in a specific direction
76
* @param direction Direction to check
77
* @return true if node has relationships in the specified direction
78
*/
79
boolean hasRelationship(Direction direction);
80
81
/**
82
* Check if this node has a specific label
83
* @param label Label to check for
84
* @return true if node has the label, false otherwise
85
*/
86
boolean hasLabel(Label label);
87
88
/**
89
* Get all labels on this node
90
* @return Iterable of labels
91
*/
92
Iterable<Label> getLabels();
93
94
/**
95
* Add a label to this node
96
* @param label Label to add
97
*/
98
void addLabel(Label label);
99
100
/**
101
* Remove a label from this node
102
* @param label Label to remove
103
*/
104
void removeLabel(Label label);
105
106
/**
107
* Get the degree (number of relationships) of this node
108
* @return Total number of relationships
109
*/
110
int getDegree();
111
112
/**
113
* Get the degree for specific relationship types
114
* @param types Relationship types to count
115
* @return Number of relationships of the specified types
116
*/
117
int getDegree(RelationshipType... types);
118
119
/**
120
* Get the degree in a specific direction
121
* @param direction Direction to count
122
* @return Number of relationships in the specified direction
123
*/
124
int getDegree(Direction direction);
125
126
/**
127
* Get the degree for specific relationship type and direction
128
* @param type Relationship type to count
129
* @param direction Direction to count
130
* @return Number of relationships of the specified type and direction
131
*/
132
int getDegree(RelationshipType type, Direction direction);
133
134
/**
135
* Get a single relationship of the specified type and direction
136
* @param type Relationship type
137
* @param direction Direction
138
* @return Single relationship, or null if none exists
139
* @throws RuntimeException if more than one relationship exists
140
*/
141
Relationship getSingleRelationship(RelationshipType type, Direction direction);
142
}
143
```
144
145
**Usage Examples:**
146
147
```java
148
import org.neo4j.graphdb.Node;
149
import org.neo4j.graphdb.Relationship;
150
import org.neo4j.graphdb.Direction;
151
import org.neo4j.graphdb.Label;
152
import org.neo4j.graphdb.RelationshipType;
153
import static org.neo4j.graphdb.Label.label;
154
import static org.neo4j.graphdb.RelationshipType.withName;
155
156
try (Transaction tx = graphDb.beginTx()) {
157
// Create nodes with labels and properties
158
Node alice = tx.createNode(label("Person"), label("Employee"));
159
alice.setProperty("name", "Alice");
160
alice.setProperty("age", 30);
161
alice.setProperty("department", "Engineering");
162
163
Node bob = tx.createNode(label("Person"));
164
bob.setProperty("name", "Bob");
165
bob.setProperty("age", 25);
166
167
// Create relationships
168
Relationship friendship = alice.createRelationshipTo(bob, withName("FRIENDS"));
169
friendship.setProperty("since", "2020-01-15");
170
friendship.setProperty("strength", 0.8);
171
172
// Label operations
173
if (alice.hasLabel(label("Employee"))) {
174
alice.addLabel(label("Manager"));
175
}
176
177
// Relationship traversal
178
for (Relationship rel : alice.getRelationships(Direction.OUTGOING)) {
179
Node other = rel.getEndNode();
180
System.out.println("Alice is connected to: " + other.getProperty("name"));
181
}
182
183
// Check relationship existence
184
if (alice.hasRelationship(withName("FRIENDS"))) {
185
System.out.println("Alice has friends");
186
}
187
188
// Get degree information
189
System.out.println("Alice has " + alice.getDegree() + " relationships");
190
191
tx.commit();
192
}
193
```
194
195
### Relationship Interface
196
197
Represents a relationship between two nodes with properties and type information.
198
199
```java { .api }
200
/**
201
* Relationship between nodes with properties and type
202
*/
203
public interface Relationship extends Entity {
204
205
/**
206
* Get the start node of this relationship
207
* @return Start node
208
*/
209
Node getStartNode();
210
211
/**
212
* Get the end node of this relationship
213
* @return End node
214
*/
215
Node getEndNode();
216
217
/**
218
* Get the other node (not the specified node) in this relationship
219
* @param node One of the nodes in this relationship
220
* @return The other node
221
* @throws RuntimeException if the provided node is not part of this relationship
222
*/
223
Node getOtherNode(Node node);
224
225
/**
226
* Get the type of this relationship
227
* @return Relationship type
228
*/
229
RelationshipType getType();
230
231
/**
232
* Check if this relationship is of a specific type
233
* @param type Type to check against
234
* @return true if relationship is of the specified type
235
*/
236
boolean isType(RelationshipType type);
237
238
/**
239
* Delete this relationship
240
*/
241
void delete();
242
}
243
```
244
245
**Usage Examples:**
246
247
```java
248
try (Transaction tx = graphDb.beginTx()) {
249
Node alice = tx.findNode(label("Person"), "name", "Alice");
250
251
// Traverse relationships
252
for (Relationship rel : alice.getRelationships()) {
253
Node other = rel.getOtherNode(alice);
254
System.out.println("Relationship: " + alice.getProperty("name") +
255
" -[:" + rel.getType().name() + "]-> " +
256
other.getProperty("name"));
257
258
// Access relationship properties
259
if (rel.hasProperty("since")) {
260
System.out.println(" Since: " + rel.getProperty("since"));
261
}
262
263
// Type checking
264
if (rel.isType(withName("FRIENDS"))) {
265
System.out.println(" This is a friendship");
266
}
267
}
268
269
tx.commit();
270
}
271
```
272
273
### Entity Interface
274
275
Base interface for nodes and relationships providing common property operations.
276
277
```java { .api }
278
/**
279
* Base interface for nodes and relationships
280
*/
281
public interface Entity {
282
283
/**
284
* Get the unique ID of this entity (deprecated - use getElementId instead)
285
* @return Entity ID
286
* @deprecated Use getElementId() for new code
287
*/
288
@Deprecated
289
long getId();
290
291
/**
292
* Get the element ID of this entity (preferred over getId)
293
* @return Element ID string
294
*/
295
String getElementId();
296
297
/**
298
* Get a property value
299
* @param key Property key
300
* @return Property value, or null if not found
301
*/
302
Object getProperty(String key);
303
304
/**
305
* Get a property value with a default
306
* @param key Property key
307
* @param defaultValue Default value if property not found
308
* @return Property value or default value
309
*/
310
Object getProperty(String key, Object defaultValue);
311
312
/**
313
* Set a property value
314
* @param key Property key
315
* @param value Property value
316
*/
317
void setProperty(String key, Object value);
318
319
/**
320
* Check if a property exists
321
* @param key Property key
322
* @return true if property exists, false otherwise
323
*/
324
boolean hasProperty(String key);
325
326
/**
327
* Remove a property
328
* @param key Property key to remove
329
* @return Previous value, or null if property didn't exist
330
*/
331
Object removeProperty(String key);
332
333
/**
334
* Get all property keys
335
* @return Iterable of property keys
336
*/
337
Iterable<String> getPropertyKeys();
338
339
/**
340
* Get all properties as a map
341
* @return Map of property key-value pairs
342
*/
343
Map<String, Object> getProperties(String... keys);
344
345
/**
346
* Get all properties as a map
347
* @return Map of all property key-value pairs
348
*/
349
Map<String, Object> getAllProperties();
350
}
351
```
352
353
### Label Interface
354
355
Interface for node labels providing categorization and indexing capabilities.
356
357
```java { .api }
358
/**
359
* Labels for categorizing nodes
360
*/
361
public interface Label {
362
363
/**
364
* Get the name of this label
365
* @return Label name
366
*/
367
String name();
368
369
/**
370
* Create a label with the specified name
371
* @param name Label name
372
* @return Label instance
373
*/
374
static Label label(String name) {
375
return org.neo4j.graphdb.Label.label(name);
376
}
377
}
378
```
379
380
### RelationshipType Interface
381
382
Interface for relationship types providing categorization and traversal filtering.
383
384
```java { .api }
385
/**
386
* Types for categorizing relationships
387
*/
388
public interface RelationshipType {
389
390
/**
391
* Get the name of this relationship type
392
* @return Type name
393
*/
394
String name();
395
396
/**
397
* Create a relationship type with the specified name
398
* @param name Type name
399
* @return RelationshipType instance
400
*/
401
static RelationshipType withName(String name) {
402
return org.neo4j.graphdb.RelationshipType.withName(name);
403
}
404
}
405
```
406
407
### Direction Enum
408
409
Enum for specifying relationship traversal direction.
410
411
```java { .api }
412
/**
413
* Direction for relationship traversal
414
*/
415
public enum Direction {
416
/** Outgoing relationships (this node is the start node) */
417
OUTGOING,
418
419
/** Incoming relationships (this node is the end node) */
420
INCOMING,
421
422
/** Both outgoing and incoming relationships */
423
BOTH
424
}
425
```
426
427
### Path Interface
428
429
Represents a path through the graph containing nodes and relationships.
430
431
```java { .api }
432
/**
433
* Represents a path through the graph
434
*/
435
public interface Path extends Iterable<Entity> {
436
437
/**
438
* Get the start node of this path
439
* @return Start node
440
*/
441
Node startNode();
442
443
/**
444
* Get the end node of this path
445
* @return End node
446
*/
447
Node endNode();
448
449
/**
450
* Get the last relationship in this path
451
* @return Last relationship, or null if path has no relationships
452
*/
453
Relationship lastRelationship();
454
455
/**
456
* Get all nodes in this path
457
* @return Iterable of nodes
458
*/
459
Iterable<Node> nodes();
460
461
/**
462
* Get all relationships in this path
463
* @return Iterable of relationships
464
*/
465
Iterable<Relationship> relationships();
466
467
/**
468
* Get the length of this path (number of relationships)
469
* @return Path length
470
*/
471
int length();
472
473
/**
474
* Reverse this path
475
* @return New path with reversed direction
476
*/
477
Path reverse();
478
}
479
```
480
481
**Usage Examples:**
482
483
```java
484
// Working with properties
485
try (Transaction tx = graphDb.beginTx()) {
486
Node node = tx.createNode(label("Person"));
487
488
// Set various property types
489
node.setProperty("name", "Alice");
490
node.setProperty("age", 30);
491
node.setProperty("active", true);
492
node.setProperty("scores", new int[]{85, 92, 78});
493
node.setProperty("metadata", Map.of("department", "Engineering", "level", "Senior"));
494
495
// Get properties with type safety
496
String name = (String) node.getProperty("name");
497
Integer age = (Integer) node.getProperty("age", 0);
498
Boolean active = (Boolean) node.getProperty("active");
499
500
// Check property existence
501
if (node.hasProperty("email")) {
502
String email = (String) node.getProperty("email");
503
}
504
505
// Iterate over all properties
506
for (String key : node.getPropertyKeys()) {
507
Object value = node.getProperty(key);
508
System.out.println(key + " = " + value);
509
}
510
511
// Get properties as map
512
Map<String, Object> properties = node.getAllProperties();
513
514
tx.commit();
515
}
516
```