使用 Cypher 在嵌入式 Neo4J 应用程序中创建节点

Creating Node in Embedded Neo4J Application with Cypher

我正在将我的系统与 neo4j 集成,使用 Cypher 查询语言创建节点对我来说很有趣,因此,作为测试,我正在尝试做这样的事情:

String path = "test.graphdb";

AbstractDatabase db = new Neo4jDatabase(path, true, false);

db.makeQuery("CREATE (n:Dog {name:'Sofia'})");
db.makeQuery("CREATE (n:Dog {name:'Laika'})");     db.makeQuery("CREATE (n:Dog {name:'Gaia'})");

Result result = db.makeQuery("MATCH (n:Dog) RETURN n");

boolean hasNext = result.hasNext();

System.out.println(hasNext);

在 Neo4jDatabase 中的什么地方 class 我有这样的 makeQuery 方法:

public Result makeQuery(String string)
{
    try(Transaction ignored = this.db.beginTx();
        Result result = this.db.execute(string) )
    {
        return result;
    }

}

不幸的是,它returns false,好像节点还没有创建!怎么了?

你自己说的,你忽略交易:)

在成功迭代结果后,您应该在事务块中调用 tx.success()

不要在已经关闭交易的情况下分发结果,其中的数据将无法在 tx 之外访问。

对于这些简单的语句,您也可以将 tx 处理留给 cypher,无需开始手动交易。

但是您必须迭代或result.close()您的结果才能完成 Cypher 操作。