Reputation: 18601
I tried to follow the documentation and I ended up with this piece of code for Neo4j 1.8:
graphDB = new GraphDatabaseFactory()
.newEmbeddedDatabaseBuilder( BASE_FOLDER + NEO4J_PATH )
.newGraphDatabase();
registerShutdownHook();
//Check if there are any indexes
System.out.println(Arrays.toString(graphDB.index().nodeIndexNames()));
Index<Node> testIndex = graphDB.index().forNodes("test");
Transaction tx = graphDB.beginTx();
try {
String nameKey = "name";
String nameValue = "Gevorg";
//The following 3 lines will be commented out
//when I run the program the second time
Node me = graphDB.createNode();
me.setProperty(nameKey, nameValue);
testIndex.add(me, nameKey, nameValue);
Node meAgain = testIndex.get(nameKey, nameValue).getSingle();
System.out.println(meAgain.getProperty(nameKey));
} finally {
tx.finish();
}
This prints the following as expected:
[] //There is no index at the very beginning
Gevorg
After the program terminates, I commented the creation of the node/index and I run the program again to hit a NullPointerException (meAgain is null). The index is retrieved correctly since the program prints [test]
first but then Node meAgain = testIndex.get(nameKey, nameValue).getSingle();
fails to retrieve the node. I tried both with and without using the Transaction. What am I doing wrong??
Upvotes: 0
Views: 482
Reputation: 6331
You need to mark your Tx as successful, before calling tx.finish
tx.success()
HTH
/peter
Upvotes: 2