0
votes

I'm beginning in Neo4j java embedded graph. I have made a first test but I cant visualize my graph in neo4j-community.

Here is my code for creating the graph :

package connection;

import java.io.File;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;

public class embededdedGraph {

    public static void main(String... args) throws Exception {
        GraphDatabaseFactory graphDbFactory = new GraphDatabaseFactory();
        File graphDir = new File("/home/nicolas/neo4j-community-3.5.14/data/databases/cars.db");
        GraphDatabaseService graphDb = graphDbFactory.newEmbeddedDatabase(graphDir);
        Transaction tx = graphDb.beginTx();

        createNode(graphDb);

        tx.close();

    }

    public static void createNode(GraphDatabaseService graphDb) {
        Node car = graphDb.createNode();
        car.addLabel(Label.label("Car"));

        car.setProperty("make", "tesla");
        car.setProperty("model", "model3");

        Node owner = graphDb.createNode(Label.label("Person"));
        owner.setProperty("firstName", "Oliver");
        owner.setProperty("lastName", "John");
        owner.createRelationshipTo(car, RelationshipType.withName("owner"));
    }

}

Next I changed the value of "#dbms.active_database" to "dbms.active_database=cars.db in the /neo4j-community-3.5.14/conf/neo4.conf file.

When I restart the neo4j-community, the database name is "cars.db" but it indicated that there are no labels and relationships in it.

What is the problem I cannot figure?

Nicolas

1
I'm not too familiar with Neo4j, but I have a working demo somewhere. Could you try to put it in a Transaction? Before you create a node, try this: Transaction tx = graphDb.beginTx(); After your code, try tx.success();Valentin Grégoire
Thanks for your answer, that solved my problem !nicope13

1 Answers

0
votes

It looks like you need to call tx.success() or tx.fail() before tx.close().

https://neo4j.com/docs/java-reference/3.5/javadocs/org/neo4j/graphdb/Transaction.html

I am also new at this API, I hope it helps