【问题标题】:Spring Data Neo4j Entity Path mappingSpring Data Neo4j 实体路径映射
【发布时间】:2015-01-14 21:26:53
【问题描述】:

我有一个独立的 Neo4j 数据库 2.1.6。我从一个基于 Web 的 Spring Boot 项目开始,并添加了 Spring Data Neo4j 3.2.1 Release。我正在尝试映射路径内的节点。我希望能够拉出不确定深度的树并将其映射到 java 实体中。

这个查询:

match p=(a:fakea)-[*]->(:fakeb) where a.aId = 1
return p;

返回两条路径:

{"start":"http://localhost:8180/db/data/node/593222","nodes":["http://localhost:8180/db/data/node/593222","http://localhost:8180/db/data/node/593223","http://localhost:8180/db/data/node/593224","http://localhost:8180/db/data/node/593225"],"length":3,"relationships":["http://localhost:8180/db/data/relationship/2489542","http://localhost:8180/db/data/relationship/2489543","http://localhost:8180/db/data/relationship/2489544"],"end":"http://localhost:8180/db/data/node/593225"}

{"start":"http://localhost:8180/db/data/node/593222","nodes":["http://localhost:8180/db/data/node/593222","http://localhost:8180/db/data/node/593223","http://localhost:8180/db/data/node/593226","http://localhost:8180/db/data/node/593227"],"length":3,"relationships":["http://localhost:8180/db/data/relationship/2489542","http://localhost:8180/db/data/relationship/2489545","http://localhost:8180/db/data/relationship/2489546"],"end":"http://localhost:8180/db/data/node/593227"}

我已经尝试使用我在此处找到的信息以几种不同的方式对其进行映射:

Spring data wth ne04j error...error while retrieving paths

@Query shortestPath return type in Spring Data Neo4j

我当前的存储库:

public interface FakeRepository extends GraphRepository<FakeA> {

@Query("match p=(a:fakea)-[*]->(:fakeb) where a.aId = {0} return p;")
public EntityPath<FakeA, FakeB> getTree(Long aId);

我也尝试过创建一个通用的抽象类:

public interface FakeRepository extends GraphRepository<FakeAbs> {

@Query("match p=(a:fakea)-[*]->(:fakeb) where a.aId = {0} return p;")
public EntityPath<FakeAbs, FakeAbs> getTree(Long aId);

我无法检索任何有用的数据。我也找不到我列出的帖子中提到的 EndResult 类。我还尝试用 Result 包装 EntityPath(也在 repo 中)。

Result<EntityPath<FakeAbs, FakeAbs>> path = fr.getTree(1l);
EntityPath<FakeAbs, FakeAbs> first = path.iterator().next();
first.endNode();

加注:

Null pointer:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
    at org.springframework.data.neo4j.support.path.ConvertingEntityPath.endNode(ConvertingEntityPath.java:112)

当我尝试检查 EntityPath 结构的任何其他部分(例如 length())时,我会收到类似的空指针。

如何查询不同深度的树路径结构并将结果映射到正确的节点实体?我特别想要路径中包含的节点。

【问题讨论】:

  • 您是在尝试获取路径信息还是希望在路径中识别特定节点?
  • 我正在尝试检索路径中的特定节点。为了清楚起见,我将编辑我原来的问题。
  • 前往此链接获取解决方案:stackoverflow.com/questions/32962032/…

标签: java neo4j spring-data-neo4j


【解决方案1】:

试试这个

而不是你现在拥有的:

public interface FakeRepository extends GraphRepository<FakeA> {

@Query("match p=(a:fakea)-[*]->(:fakeb) where a.aId = {0} return p;")
public EntityPath<FakeA, FakeB> getTree(Long aId);

用途:

public interface FakeRepository extends GraphRepository<FakeA> {

@Query("start p=node{0} match (p)-[*]->(a:fakea) return a;")
public FakeA getTree(FakeA fakeA);

该查询将采用 FakeA 的实例并找到与其关联的 FakeA,假设在这种情况下只有一个匹配项。如果可以有多个,则更改 meth 方法签名以返回 Set。

但是,我认为您也太努力了,没有利用 Spring Data 的内置动态查找器。我建议您阅读这个不错的教程:Getting Started With Spring Data and Neo4J

【讨论】:

  • 我最终使用的解决方案是返回单个对象并使用 Relatedto 和 Fetch 注释映射我需要的关系。从那以后,我发现这是实现这一目标的较慢方法之一。 jexp.de/blog/2014/12/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-05
  • 1970-01-01
  • 1970-01-01
  • 2020-02-21
  • 2023-03-18
相关资源
最近更新 更多