gpt4 book ai didi

neo4j - 在 spring-data-neo4j 中将 NodeProxy 转换为 NodeEntity

转载 作者:行者123 更新时间:2023-12-04 04:53:35 25 4
gpt4 key购买 nike

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

异常(exception)
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 接口(interface)中的 convert 方法将返回的对象转换为其正确的类;这是一个例子:

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

Neo4jOperations 对象由 Spring Data Neo4j 基础设施使用 @Autowired 注解注入(inject)。

关于neo4j - 在 spring-data-neo4j 中将 NodeProxy 转换为 NodeEntity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17072699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com