我使用Neo4jClient执行以下查询,以通过路径检索节点和关系。
public Collection<Map<String, Object>> findBookByUniqueIds(List<String> srcUniqueIds, String name) {
return neo4jClient.query(
"""
MATCH path = (src:Book)-[*1..2]-(target:Book)
WHERE src.uniqueId IN $uniqueIds
AND toUpper(target.name) = toUpper($name)
RETURN nodes(path) AS nodes, relationships(path) AS relations
""")
.bind(srcUniqueIds).to("uniqueIds")
.bind(name).to("name")
.fetch().all();
}
字符串
我提取数据如下:
Collection<Map<String, Object>> result = findBookByUniqueIds(ids, name);
List<Object> nodes = (List<Object>) result.get("nodes");
List<Object> relationships = (List<Object>) result.get("relations");
型
当我试图将节点cask回实体类时:
if (nodes.size() == 2) {
Book book1 = (Book) nodes.get(0);
Book book2 = (Book) nodes.get(1);
} else if (nodes.size() == 3) {
Book book1 = (Book) nodes.get(0);
Author author = (Author) nodes.get(1);
Book book2 = (Book) nodes.get(2);
}
型
我得到了例外java.lang.ClassCastException: class org.neo4j.driver.internal.InternalNode cannot be cast to class xxx
如何将节点转换回实体类?
谢谢
1条答案
按热度按时间toe950271#
我不确定这样做是否正确,但我自己也遇到过这个问题,没有找到比简单地创建所需类的新对象并使用从
InternalNode
中提取的值填充它更好的解决方案。举例来说:
字符串