我正在使用 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");
当我尝试将节点装桶回实体类时:
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
关于如何将节点转换回实体类有什么想法吗?
谢谢
我不确定我做得是否正确,但我自己也遇到过这个问题,并且没有找到比简单地创建我需要的类的新对象并用我从中提取的值填充它更好的解决方案
InternalNode
。
例如:
InternalNode internalNode = (InternalNode) weightedCriteriaArrayElement.get("criterion");
Criterion criterion = new Criterion(); // my class
Map<String, Object> nodeProperties = new HashMap<>(internalNode.asMap());
BeanUtils.populate(criterion, nodeProperties);
criterion.setGraphId(internalNode.id());