我已经安装了 Neo4j
的社区版 3.5.0-alpha07
,并且我正在尝试创建一个简单的图
,其中包含以下两个节点this教程。问题是来自 org.neo4j.graphdb.Transaction
的关键字 Transaction
显示语法错误
。代码如下:
Operation.java
import java.io.File;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
public class Operation {
//private static final File file =new File();
public static void main(String[] args) {
GraphDatabaseFactory dbFactory = new GraphDatabaseFactory();
GraphDatabaseService db= dbFactory.newEmbeddedDatabaseBuilder("C:\\Users\\pritom.mazumdar\\Downloads\\neo4j-community-3.5.0-alpha07\\data\\databases\\graph.db").newGraphDatabase();
//Transaction tx = db.beginTx();
try (Transaction tx = db.beginTx()) {
The resource type Transaction does not implement java.lang.AutoCloseable
Node javaNode = db.createNode();
javaNode.setProperty("TutorialID", "JAVA001");
javaNode.setProperty("Title", "Learn Java");
javaNode.setProperty("NoOfChapters", "25");
javaNode.setProperty("Status", "Completed");
Node scalaNode = db.createNode();
scalaNode.setProperty("TutorialID", "SCALA001");
scalaNode.setProperty("Title", "Learn Scala");
scalaNode.setProperty("NoOfChapters", "20");
scalaNode.setProperty("Status", "Completed");
Relationship relationship = javaNode.createRelationshipTo(scalaNode, (RelationshipType) Label.label("JVM_LANG"));
relationship.setProperty("Id","1234");
relationship.setProperty("OOPS","YES");
relationship.setProperty("FP","YES");
tx.success();
}
System.out.println("Done successfully");
}
}
我已经尝试实现AutoCloseable
并覆盖
close
方法,但它仍然不起作用,语法错误仍然存在。
最佳答案
这是因为 try-with-resources 只接受 AutoCloseable
对象。编译器说 Transaction
不是 AutoCloseable
。
关于java - Neo4j 3.5 中的事务未实现 java.lang.AutoCloseable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51935200/