2012-10-05 150 views
0

我试图按照文档和我结束了这段代码为Neo4j的1.8:Neo4j的索引没有找到节点

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(); 
} 

这将打印以下预期:

[] //There is no index at the very beginning 
Gevorg 

后,程序终止,我评论了节点/索引的创建,然后再次运行该程序以触发NullPointerException(meAgain为null)。由于程序首先打印[test],但是Node meAgain = testIndex.get(nameKey, nameValue).getSingle();未能检索到节点,因此索引被正确检索。我尝试使用和不使用交易。我究竟做错了什么??

回答

2

你需要调用tx.finish

tx.success() 

HTH

/彼得

之前标记您的Tx一样成功,