gpt4 book ai didi

java - Neo4j 索引未找到节点

转载 作者:行者123 更新时间:2023-12-02 07:32:17 24 4
gpt4 key购买 nike

我尝试遵循文档,最终得到了 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 为 null)。由于程序首先打印 [test] 但随后 Node meAgain = testIndex.get(nameKey, nameValue).getSingle(); 无法检索节点,因此可以正确检索索引。我尝试了使用和不使用事务的情况。我做错了什么??

最佳答案

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

tx.success()

HTH

/彼得

关于java - Neo4j 索引未找到节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12738501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com