Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add label to node using embedded API

Tags:

java

neo4j

Using Neo4j 2.0 milestone 3

Currently have this code (working code)

String DB_PATH = "/usr/local/Cellar/neo4j/community-1.8.1-unix/libexec/data/graph.db";
GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
Transaction tx = graphDb.beginTx();
try {
   Node myNode = graphDb.createNode();

tx.success();
}
finally {
   tx.finish();
}

This is the embedded API. How can I add a label to my node? Thanks!

like image 602
Badmiral Avatar asked Jun 14 '13 15:06

Badmiral


1 Answers

You have to create a label first by creating an Enum that implements Label, or use DynamicLabel to create one on the fly.

Once you have created, you will have to add it to the Node.

The below shows you how to do it with DynamicLabel:

import org.neo4j.graphdb.DynamicLabel;

Label myLabel = DynamicLabel.label("Label_Name");
myNode.addLabel(myLabel);

You also have to do this within a Transaction.

like image 166
Nicholas Avatar answered Oct 11 '22 14:10

Nicholas