I access the neo4j database via GraphDatabaseService. I added some nodes and relation and want to do a query. My goal is to get all Nodes that got an Calling relation to the endnode. So i have the whole paths to this node. (e.g.: node1 -calling-> node2 -calling*-> nodeX -calling-> endnode)
I tried something like this:
GraphDatabaseService _graphDatabase; // db is running and working fine
Node node;//given by the function call
HashMap<String, Object> properties = Maps.newHashMap();
properties.put("name", getPropertyFromNode(node, "name"));
String query = "MATCH p=(startnode)-[rel:CALLING*]->(endnode) WHERE endnode.name = {name} RETURN p";
Result result = _graphDatabase.execute(query, properties);
while (result.hasNext()) {
Map<String, Object> next = result.next();
next.forEach((k, v) -> {
System.out.println(k + " : " + v.toString());
if (v instanceof PathImpl) {
PathImpl pathImpl = (PathImpl) v;
String startNodeName = getPropertyFromNode(pathImpl.startNode(), "name");
String endNodeName = getPropertyFromNode(pathImpl.endNode(), "name");
System.out.println(startNodeName + " -calling-> " + endNodeName);
}
});
}
public String getPropertyFromNode(Node node, String propertyName) {
String result = null;
try (Transaction transaction = _graphDatabase.beginTx()) {
result =node.getProperty(propertyName).toString();
transaction.success();
}
return result;
}
Problem for me is, that the result is a Map<'String, Object>, and i want do get the id's or names of the nodes that are in the relation. I tried to cast the Object to PathImpl because thats the type that is returned (figured out by debug), but it seams like that this class is in a different class loader so the statement instanceof returns false. v.toString() returns a String representation of the liked nodes with id's (e.g.: p : (24929)--[CALLING,108061]-->(24930))
My question is, can i access this information in a better way or how can i change the classLoader (if that is the problem, i'm working with eclipse in a gradle project) to make that cast happened. I don't wanna parse the String to get the id's and get the nodes from the db via that property, its seams ugly for me.