我在使用 Spring 数据 neo4j 的 Spring boot 应用程序中进行了以下查询。当我将查询作为独立的密码查询运行时,它会返回我正在查找的所有数据,但是,当我在 Spring Boot 应用程序中运行相同的查询时,它不会返回我需要的所有数据。它只有一层或两层深。
MATCH path = (stepNode:StepNode {stepNodeId: "<id_goes_here>"})-[:PARENT*0..]-(descendant)
WITH collect(path) AS paths, stepNode, collect(distinct descendant) AS descendants
UNWIND descendants AS d
OPTIONAL MATCH (d)-[r]->(related)
RETURN stepNode, paths, collect(distinct r) AS relationships, collect(distinct related) AS relatedNodes;
我有什么遗漏的吗?
这是Spring Boot存储库中的查询
@Query("""
MATCH path = (stepNode:StepNode {stepNodeId: "421d419a-05a5-4cd6-81f5-3fe5336eead0"})-[:PARENT*0..]-(descendant)
WITH collect(path) AS paths, stepNode, collect(distinct descendant) AS descendants
UNWIND descendants AS d
OPTIONAL MATCH (d)-[r]->(related)
RETURN stepNode, paths, collect(distinct r) AS relationships, collect(distinct related) AS relatedNodes;
""")
Optional<StepNode> findTreeByRootNodeId(UUID rootNodeId);
我还注意到,在我的日志中,就在执行查询之前,Spring Data Neo4j 显示了一些与我能够从相关实体检索的数据的 element_ids 相匹配的数据。下面是在我的查询运行之前提取的数据
2024-06-22T13:57:45.591Z DEBUG 752174 --- [Game Engine] [nio-9000-exec-1] org.springframework.data.neo4j.cypher : Executing:
MATCH (rootNodeIds:`Product`) WHERE elementId(rootNodeIds) IN $rootNodeIds WITH collect(rootNodeIds) AS n OPTIONAL MATCH ()-[relationshipIds]-() WHERE elementId(relationshipIds) IN $relationshipIds WITH n, collect(DISTINCT relationshipIds) AS __sr__ OPTIONAL MATCH (relatedNodeIds) WHERE elementId(relatedNodeIds) IN $relatedNodeIds WITH n, __sr__ AS __sr__, collect(DISTINCT relatedNodeIds) AS __srn__ UNWIND n AS rootNodeIds WITH rootNodeIds AS product, __sr__, __srn__ RETURN product AS __sn__, __sr__, __srn__
2024-06-22T13:57:45.591Z TRACE 752174 --- [Game Engine] [nio-9000-exec-1] org.springframework.data.neo4j.cypher : with parameters:
:param rootNodeIds => ['4:e980bb06-70fe-4527-9e8e-20567526a367:547']
:param relatedNodeIds => ['4:e980bb06-70fe-4527-9e8e-20567526a367:551', '4:e980bb06-70fe-4527-9e8e-20567526a367:557', '4:e980bb06-70fe-4527-9e8e-20567526a367:555', '4:e980bb06-70fe-4527-9e8e-20567526a367:553', '4:e980bb06-70fe-4527-9e8e-20567526a367:556', '4:e980bb06-70fe-4527-9e8e-20567526a367:554', '4:e980bb06-70fe-4527-9e8e-20567526a367:552', '4:e980bb06-70fe-4527-9e8e-20567526a367:561', '4:e980bb06-70fe-4527-9e8e-20567526a367:559', '4:e980bb06-70fe-4527-9e8e-20567526a367:556', '4:e980bb06-70fe-4527-9e8e-20567526a367:554', '4:e980bb06-70fe-4527-9e8e-20567526a367:553', '4:e980bb06-70fe-4527-9e8e-20567526a367:557', '4:e980bb06-70fe-4527-9e8e-20567526a367:555', '4:e980bb06-70fe-4527-9e8e-20567526a367:553', '4:e980bb06-70fe-4527-9e8e-20567526a367:556', '4:e980bb06-70fe-4527-9e8e-20567526a367:554', '4:e980bb06-70fe-4527-9e8e-20567526a367:552', '4:e980bb06-70fe-4527-9e8e-20567526a367:551', '4:e980bb06-70fe-4527-9e8e-20567526a367:556', '4:e980bb06-70fe-4527-9e8e-20567526a367:554', '4:e980bb06-70fe-4527-9e8e-20567526a367:552', '4:e980bb06-70fe-4527-9e8e-20567526a367:556', '4:e980bb06-70fe-4527-9e8e-20567526a367:554', '4:e980bb06-70fe-4527-9e8e-20567526a367:551', '4:e980bb06-70fe-4527-9e8e-20567526a367:561', '4:e980bb06-70fe-4527-9e8e-20567526a367:559']
:param relationshipIds => ['5:e980bb06-70fe-4527-9e8e-20567526a367:96', '5:e980bb06-70fe-4527-9e8e-20567526a367:291', '5:e980bb06-70fe-4527-9e8e-20567526a367:342', '5:e980bb06-70fe-4527-9e8e-20567526a367:287', '5:e980bb06-70fe-4527-9e8e-20567526a367:275', '5:e980bb06-70fe-4527-9e8e-20567526a367:341', '5:e980bb06-70fe-4527-9e8e-20567526a367:274', '5:e980bb06-70fe-4527-9e8e-20567526a367:345', '5:e980bb06-70fe-4527-9e8e-20567526a367:344', '5:e980bb06-70fe-4527-9e8e-20567526a367:289', '5:e980bb06-70fe-4527-9e8e-20567526a367:343', '5:e980bb06-70fe-4527-9e8e-20567526a367:306', '5:e980bb06-70fe-4527-9e8e-20567526a367:305', '5:e980bb06-70fe-4527-9e8e-20567526a367:326']
2024-06-22T13:57:45.653Z DEBUG 752174 --- [Game Engine] [nio-9000-exec-1] o.s.d.n.c.t.Neo4jTransactionManager : Initiating transaction commit
2024-06-22T13:57:45.662Z INFO 752174 --- [Game Engine] [nio-9000-exec-1] c.l.d.g.c.game.TemplateController : [8A26299413CCEF2FB715D1D610E91EF6] product found, about to check for root node
2024-06-22T13:57:45.662Z DEBUG 752174 --- [Game Engine] [nio-9000-exec-1] o.s.d.n.c.t.Neo4jTransactionManager : Creating new transaction with name [com.lucidarray.d_and_c.game_engine.services.game.StepNodeService.findTreeById]: PROPAGATION_REQUIRES_NEW,ISOLATION_DEFAULT; 'transactionManager'
2024-06-22T13:57:45.674Z DEBUG 752174 --- [Game Engine] [nio-9000-exec-1] o.s.d.n.c.t.Neo4jTransactionManager : Participating in existing transaction
2024-06-22T13:57:45.674Z DEBUG 752174 --- [Game Engine] [nio-9000-exec-1] org.springframework.data.neo4j.cypher : Executing:
MATCH path = (stepNode:StepNode {stepNodeId: "421d419a-05a5-4cd6-81f5-3fe5336eead0"})-[:PARENT*0..]-(descendant)
WITH collect(path) AS paths, stepNode, collect(distinct descendant) AS descendants
UNWIND descendants AS d
OPTIONAL MATCH (d)-[r]->(related)
RETURN stepNode, paths, collect(distinct r) AS relationships, collect(distinct related) AS relatedNodes;
2024-06-22T13:57:45.675Z TRACE 752174 --- [Game Engine] [nio-9000-exec-1] org.springframework.data.neo4j.cypher : with parameters:
:param 0 => "421d419a-05a5-4cd6-81f5-3fe5336eead0"
:param rootNodeId => "421d419a-05a5-4cd6-81f5-3fe5336eead0"
2024-06-22T13:57:45.705Z DEBUG 752174 --- [Game Engine] [nio-9000-exec-1] o.s.d.n.c.t.Neo4jTransactionManager : Initiating transaction commit
直接在 cypher 中运行查询时获得的数据(我使用表格,因为它可能比图表更容易比较)
我正在使用
spring boot 3.2.5
以及与之兼容的 spring-boot-starter-data-neo4j
如有任何帮助,我们将不胜感激
您的
@Query
完全忽略了传递给$rootNodeId
的findTreeByRootNodeId()
参数。
如果
$rootNodeId
应该用作 stepNodeId
值,请尝试以下操作:
@Query("""
MATCH path = (stepNode:StepNode {stepNodeId: $rootNodeId})-[:PARENT*0..]-(descendant)
WITH collect(path) AS paths, stepNode, collect(distinct descendant) AS descendants
UNWIND descendants AS d
OPTIONAL MATCH (d)-[r]->(related)
RETURN stepNode, paths, collect(distinct r) AS relationships, collect(distinct related) AS relatedNodes;
""")
Optional<StepNode> findTreeByRootNodeId(UUID rootNodeId);