0

I am failing to execute the cipher query through java while creating a relationship. It is not throwing any error, but no relationships are created. Tried with other cipher query to return a node, which is returning perfectly. But unable to figure-out the reason for relationships not being created.

Here goes my code:

public static void addrelation(String node1, String node2, int cocr) {
        try (Transaction tx = graphdb.beginTx();
                Result result = graphdb.execute("MATCH(a{word:\"" + node1
                        + "\"}),(b{word:\"" + node2 + "\"})"
                        + "CREATE a-[r:coocr{val:" + cocr + "}]->(b)"
                        + "CREATE a<-[s:coocr{val:" + cocr + "}]-(b)"
                        + "RETURN r,s")) {
                }
            } 
Jack Daniel
  • 2,527
  • 3
  • 31
  • 52
  • As an aside you should use parameters for performance, security, and to avoid string concatenation headaches: http://neo4j.com/docs/stable/cypher-parameters.html – Brian Underwood Oct 19 '15 at 12:41

2 Answers2

1

Adding tx.success() line made changes into the database.

    public static void addrelation(String node1, String node2, int cocr) {
            try (Transaction tx = graphdb.beginTx();
                    Result result = graphdb.execute("MATCH(a{word:\"" + node1
                            + "\"}),(b{word:\"" + node2 + "\"})"
                            + "CREATE a-[r:coocr{val:" + cocr + "}]->(b)"
                            + "CREATE a<-[s:coocr{val:" + cocr + "}]-(b)"
                            + "RETURN r,s")) {

   //Adding this line resolved the issue.                 
                           tx.success();

                    }
                } 
Jack Daniel
  • 2,527
  • 3
  • 31
  • 52
0

Is your MATCH matching anything? If not it won't created the relationships and will return successfully. Maybe remove the two CREATE lines and RETURN a, b to see.

Brian Underwood
  • 10,746
  • 1
  • 22
  • 34