我有下面的Cypher查询。它返回球员列表和每个球员所参加的所有联赛列表。现在对于每个返回的球员,我想创建Person
NodeEntity
而不是使用NodeProxy
。我想知道什么是有效的方法。
String q = "START t=node({teamId}) MATCH player-[:PLAYED_WITH_TEAM]->t-[:CONTESTED_IN]->league WITH player AS player, league.startDate AS startDate, league.name AS leagueName ORDER BY startDate RETURN player, collect(leagueName) AS leagueNames";
Map<String, Object> params = Maps.newHashMap();
params.put("teamId", selectedTeam);
Result<Map<String, Object>> result = template.query(q, params);
final List<Player> players = new ArrayList<Player>();
result.handle(new Handler<Map<String, Object>>()
{
@Override
public void handle(Map<String, Object> value)
{
players.add((Player) value.get("player"));
}
});
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/avl] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: org.neo4j.kernel.impl.core.NodeProxy cannot be cast to com.aravind.avl.domain.Player] with root cause
java.lang.ClassCastException: org.neo4j.kernel.impl.core.NodeProxy cannot be cast to com.aravind.avl.domain.Player
at com.aravind.avl.controller.RegistrationController$1.handle(RegistrationController.java:103)
您应该使用Neo4jOperations接口中的convert方法将返回的对象转换为其适当的类;下面是一个示例:
neo4jOperations.convert(value.get("player"), Player.class);
neo4jOperations对象由Spring Data Neo4j基础设施使用@Autowired注释注入。