SDN 4 - Neo4jOperation.convert() deprecated

时间:2015-06-26 09:32:10

标签: neo4j spring-data-neo4j spring-data-neo4j-4

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 个答案:

答案 0 :(得分:2)

正如Luanne所说,你现在需要分两步完成。对于存储库方法,您可以尝试这样的事情:

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

这应该以正确的顺序返回你想要的Person个实体,虽然我很感激实际的计数不会被映射,所以你不得不发出第二个查询来查找计数。不幸的是。

如果您不需要实际的Person实体,而只需要这些节点的某些属性,那么解决方法可能是映射到@QueryResult对象。像这样:

@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);

...其中PersonQueryResult是一个用@QueryResult注释的POJO,其setter / getters对应于查询的return子句中列出的属性。

答案 1 :(得分:1)

对于此用例,您必须返回ID(人物)并使用repository.findOneneo4jOperations.load代替。