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

pu3pd22g  于 2024-01-07  发布在  Spring
关注(0)|答案(1)|浏览(251)

我使用Neo4jClient执行以下查询,以通过路径检索节点和关系。

  1. public Collection<Map<String, Object>> findBookByUniqueIds(List<String> srcUniqueIds, String name) {
  2. return neo4jClient.query(
  3. """
  4. MATCH path = (src:Book)-[*1..2]-(target:Book)
  5. WHERE src.uniqueId IN $uniqueIds
  6. AND toUpper(target.name) = toUpper($name)
  7. RETURN nodes(path) AS nodes, relationships(path) AS relations
  8. """)
  9. .bind(srcUniqueIds).to("uniqueIds")
  10. .bind(name).to("name")
  11. .fetch().all();
  12. }

字符串
我提取数据如下:

  1. Collection<Map<String, Object>> result = findBookByUniqueIds(ids, name);
  2. List<Object> nodes = (List<Object>) result.get("nodes");
  3. List<Object> relationships = (List<Object>) result.get("relations");


当我试图将节点cask回实体类时:

  1. if (nodes.size() == 2) {
  2. Book book1 = (Book) nodes.get(0);
  3. Book book2 = (Book) nodes.get(1);
  4. } else if (nodes.size() == 3) {
  5. Book book1 = (Book) nodes.get(0);
  6. Author author = (Author) nodes.get(1);
  7. Book book2 = (Book) nodes.get(2);
  8. }


我得到了例外java.lang.ClassCastException: class org.neo4j.driver.internal.InternalNode cannot be cast to class xxx
如何将节点转换回实体类?
谢谢

toe95027

toe950271#

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

  1. InternalNode internalNode = (InternalNode) weightedCriteriaArrayElement.get("criterion");
  2. Criterion criterion = new Criterion(); // my entity class
  3. Map<String, Object> nodeProperties = new HashMap<>(internalNode.asMap());
  4. BeanUtils.populate(criterion, nodeProperties);
  5. criterion.setGraphId(internalNode.id());

字符串

相关问题