Hi!
I have written quite a bit of stored procedures for neo 3.5, so i started looking at porting it to 4.0.
So Ive tried to make a very simple function to merge a node, just to try out, and then return the node.
If I dont return the node, it works fine, but i really dont understand what is wrong with the code. The unit test i wrote works. Any help/explanation would be very helpful!
To return the node I have stolen the NodeResult class from apoc:
import org.neo4j.graphdb.Node;
public class NodeResult {
public final Node node;
public NodeResult(final Node node) {
this.node = node;
}
@Override
public boolean equals(final Object o) {
return this == o || o != null && getClass() == o.getClass() && node.equals(((NodeResult) o).node);
}
@Override
public int hashCode() {
return node.hashCode();
}
}
and this is my very simple merge function:
package test;
import org.neo4j.graphdb.;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.logging.Log;
import org.neo4j.procedure.;
import java.util.stream.Stream;
import test.NodeResult;
public class Insert {
// This field declares that we need a GraphDatabaseService
// as context when any procedure in this class is invoked
@Context
public GraphDatabaseService db;
@Context
public Log log;
@Procedure(name = "test.merge", mode = Mode.WRITE)
@Description("CALL test.merge(label, id)")
public Stream<NodeResult> merge(@Name(value = "label") String label,
@Name(value = "id") Long id){
Node node;
try (Transaction tx = db.beginTx()){
Label l = Label.label(label);
node = tx.findNode(l, "id", id);
if(node == null){
node = tx.createNode();
node.addLabel(l);
}
tx.commit();
return Stream.of(new NodeResult(node));
}
}
}
Thanks!