2
votes

With SDN 3 it was possible to use Neo4jOperations.convert(Object value, Class type) to convert results from a cypher query which returns Iterable<Map<String, Object>> to a Neo4j domain class (annotated with @NodeEntity). For example:

Map<String,Object> results = repository.findSomething("John");
for(Map<String,Object> row : results) {
    Person person = neo4jOperations.convert(row.get("person"), Person.class);
    ...
}

// Repository method
@Query("MATCH (person:Person)-[rel]->(node) WHERE person.firstName = {firstName}  RETURN DISTINCT person, COUNT(rel) ORDER BY COUNT(rel)"
Iterable<Map<String,Object>> findSomething(@Param("firstName") String firstName);

As T convert(Object value, Class type) no longer exists in Neo4jOperations in SDN 4, what's the equivalence for this in SDN 4?

http://docs.spring.io/spring-data/neo4j/docs/4.0.0.M1/reference/html/#reference_programming_model_simple-mapping doesn't cover how the mapping/conversion is done explicitly or implicitly.

I'm using the snapshots build.

Any help much appreciated.

2

2 Answers

2
votes

As Luanne suggests, you'll need to do this in two steps at the moment. For the repository method you could try something like this instead:

@Query("MATCH (p:Person)-[rel]->(node) WHERE p.firstName = {firstName} RETURN DISTINCT p ORDER BY COUNT(rel)")
Iterable<Person> findSomething(@Param("firstName") String firstName);

This should return the Person entities you want in the correct order, although I appreciate the actual count won't be mapped so you'd have to issue a second query to find the count, unfortunately.

If you don't need the actual Person entities but rather just some properties of these nodes, then a work-around may be to map to a @QueryResult object instead. Something like this:

@Query("MATCH (p:Person)-[rel]->(node) WHERE p.firstName = {firstName} RETURN DISTINCT p.firstName, p.surname, p.dateOfBirth, COUNT(rel) AS rank ORDER BY rank")
Iterable<PersonQueryResult> findSomething(@Param("firstName") String firstName);

...where PersonQueryResult is a POJO annotated with @QueryResult with setters/getters corresponding to the properties listed in the query's return clause.

1
votes

For this use case, you'll have to return ID(person) and use the repository.findOne or neo4jOperations.load instead.