0

我尝试按照文档进行操作,最终得到了 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();
}

这将按预期打印以下内容:

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

程序终止后,我注释了节点/索引的创建并再次运行程序以命中 NullPointerException(meAgain 为空)。索引被正确检索,因为程序[test]首先打印但随后Node meAgain = testIndex.get(nameKey, nameValue).getSingle();无法检索节点。我尝试了使用和不使用事务。我究竟做错了什么??

4

1 回答 1

2

在调用之前,您需要将您的 Tx 标记为成功tx.finish

tx.success()

高温高压

/彼得

于 2012-10-05T04:32:12.780 回答