我正在使用Neo4j 2.1.2社区版。当我将neo4j指向我创建的数据库并登录到webadmin时,它会显示我在代码中创建的关系类型和标签,但是当我尝试获取节点和关系时,它不会列出任何内容。
请参阅随附的屏幕截图。
我正在使用以下代码创建数据库。
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 HelloWorld {
private GraphDatabaseService m_databaseService;
private Node m_firstNode;
private Node m_secondNode;
private Relationship m_relationship;
public static void main(String[] args) {
new HelloWorld().startApplication();
}
private void startApplication() {
createDatabase();
createNodesAndrelationship();
shutDownDB();
}
private void createDatabase() {
m_databaseService = new GraphDatabaseFactory().newEmbeddedDatabase("E:\\ERM\\RoutingData\\neo4j");
}
private void createNodesAndrelationship() {
Transaction tx = m_databaseService.beginTx();
try {
m_firstNode = m_databaseService.createNode();
m_firstNode.addLabel(new Label() {
@Override
public String name() {
return "Demo";
}
});
m_firstNode.setProperty("message", "Hello");
System.out.println("Created First Node.");
m_secondNode = m_databaseService.createNode();
m_secondNode.setProperty("message", "world !");
m_secondNode.addLabel(new Label() {
@Override
public String name() {
return "Demo";
}
});
System.out.println("Created Second Node.");
m_relationship = m_firstNode.createRelationshipTo(m_secondNode, RelType.KNOWS);
m_relationship.setProperty("message", "bravo neo4j !");
System.out.println("Created relationship.");
tx.success();
} finally {
tx.failure();
}
System.out.println(m_firstNode.getProperty("message").toString() + " " + m_relationship.getProperty("message") + " " + m_secondNode.getProperty("message"));
}
private void shutDownDB() {
m_databaseService.shutdown();
System.out.println("Database shutdown completed.");
}
private static enum RelType implements RelationshipType {
KNOWS
}
}
请说明我做错了什么。
答案 0 :(得分:1)
您正在回滚交易而不是提交交易。
将tx.failure()
替换为tx.close()
。
此外,此行需要在tx.close()
来电之前移动,以避免org.neo4j.graphdb.NotInTransactionException
:
System.out.println(m_firstNode.getProperty("message").toString() + " " + m_relationship.getProperty("message") + " " + m_secondNode.getProperty("message"));