Spring Data Neo4j在检索路径时无法将InternalNode转换回实体类

pu3pd22g  于 9个月前  发布在  Spring
关注(0)|答案(1)|浏览(175)

我使用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
如何将节点转换回实体类?
谢谢

toe95027

toe950271#

我不确定这样做是否正确,但我自己也遇到过这个问题,没有找到比简单地创建所需类的新对象并使用从InternalNode中提取的值填充它更好的解决方案。
举例来说:

InternalNode internalNode = (InternalNode) weightedCriteriaArrayElement.get("criterion");

Criterion criterion = new Criterion(); // my entity class
Map<String, Object> nodeProperties = new HashMap<>(internalNode.asMap());
BeanUtils.populate(criterion, nodeProperties);
criterion.setGraphId(internalNode.id());

字符串

相关问题