Suresh Payankannur

Wednesday, May 28, 2014

Executing Cypher Queries using Spring Data Neo4jTemplate

In addition to standard CRUD methods, provides the ability to execute Cypher queries. The following example shows how to use Neo4jTemplate:
@ContextConfiguration(locations = {"classpath:/test-config.xml"})
public class QueryTest extends AbstractTestNGSpringContextTests {
    public void testQuery() {
        Object result = template.exec(new GraphCallback<Object>() {
                public Object doWithGraph(GraphDatabase graph) throws Exception {
                    QueryEngine engine = graph.queryEngine();
                    Result<Map<String, Object>> result
                        = engine.query("match n return n", null);

                    for (Map<String, Object> m : result) {
                        for (Map.Entry<String, Object> e : m.entrySet()) {
                            Node node = (Node) e.getValue();
                            System.out.println("===>>" + node.getId());
                            System.out.println("===>>" + node.getLabels());

                    return null;
    private Neo4jTemplate template;


Post a Comment

Blog Archive

Scroll To Top