Suresh Payankannur

Wednesday, May 28, 2014

Executing Cypher Queries using Spring Data Neo4jTemplate


In addition to standard CRUD methods, org.springframework.data.neo4j.support.Neo4jTemplate 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 {
    @Test
    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;
                }
            });
    }
    @Autowired
    private Neo4jTemplate template;
}


0 comments:

Post a Comment

Blog Archive

Scroll To Top