I am using Neo4j 2.1.2 community edition. When I point neo4j to the database I created and login to webadmin, it shows the relationship type and and Labels I created in my code but when i try to fetch the nodes and relationship it does not list any thing.
See the screenshot attached.
I am creating my database using following piece of code.
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
}
}
Please suggest what am I doing wrong.
You are rolling back the transaction rather than committing it.
Replace tx.failure()
with tx.close()
.
Also, this line needs to be moved before the tx.close()
call, to avoid an org.neo4j.graphdb.NotInTransactionException
:
System.out.println(m_firstNode.getProperty("message").toString() + " " + m_relationship.getProperty("message") + " " + m_secondNode.getProperty("message"));