【问题标题】:converting a NodeProxy to NodeEntity in spring-data-neo4j在 spring-data-neo4j 中将 NodeProxy 转换为 NodeEntity
【发布时间】:2013-06-08 23:46:13
【问题描述】:

我有以下 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)

【问题讨论】:

  • 您在事务性上下文中吗?

标签: neo4j spring-data spring-data-neo4j spring-data-graph


【解决方案1】:

您应该使用 Neo4jOperations 接口中的 convert 方法将返回的对象转换为适当的类;这是一个例子:

neo4jOperations.convert(value.get("player"), Player.class);

neo4jOperations 对象由 Spring Data Neo4j 基础架构使用 @Autowired 注解注入。

【讨论】:

  • neo4jOperations 没有被注入,它在我的服务中为空。
  • 如何在SDN 4中实现?
  • 从 SDN 4 开始,Neo4jOperations 已被弃用,请通读 SDN 4 文档中的paragraph 11.3 以了解如何收集查询结果。
猜你喜欢
  • 2018-08-30
  • 2013-09-23
  • 2019-02-17
  • 1970-01-01
  • 1970-01-01
  • 2015-07-19
  • 2016-01-05
  • 1970-01-01
  • 2013-03-06
相关资源
最近更新 更多