Neo4j OGM Neo4jSession 变量替换查询失败

Neo4j OGM Neo4jSession variable substitution queries fail

对我来说:

neo4jSession.query("MATCH (n:Widget) WHERE (n.partNumber STARTS WITH '001') RETURN n.partNumber AS id, n.name AS description, n.urn AS urn LIMIT 10", Collections.emptyMap());

有效。

此查询无效:

    String query = "MATCH (n:Widget) " +
    "WHERE (n.partNumber STARTS WITH {queryString}) " +
    "RETURN n.partNumber AS id, n.name AS description, n.urn AS urn " +
    "LIMIT {limit}";
Map<String, Object> params = ImmutableMap
    .<String, Object>builder()
    .put("queryString", queryString)
    .put("limit", limit)
    .build();
return (List) neo4jOperations.queryForObjects(Object.class, query, params);

它returns 一个空列表。我也尝试过使用我的实际域对象:

 return (List) neo4jOperations.queryForObjects(Widget.class, query, params);

同样的结果。

我正在使用 OGM 2.0.2、neo4j 2.3.2 和 Spring Data Neo4j 4.1.1 但我已经在没有 neo4jOperations 的情况下单独使用 Neo4jSession 进行了尝试,结果相同。哦,我还使用了带有 HTTP 驱动程序的 neo4j 的删除实例。

OGM 是否存在错误?

更多信息:

通过网络,我认为消息是这样的:

{      "statements":[  
      {  
         "statement":"MATCH (n:Widget) WHERE (n.partNumber STARTS WITH {queryString}) RETURN n.partNumber AS id, n.name AS description, n.urn AS urn LIMIT {limit}",
         "parameters":{  
            "queryString":"001",
            "limit":10
         },
         "resultDataContents":[  
            "graph"
         ],
         "includeStats":false
      }    ] }




{      "statements":[  
      {  
         "statement":"MATCH (n:Widget) WHERE (n.partNumber STARTS WITH '001') RETURN n.partNumber AS id, n.name AS description, n.urn AS urn LIMIT 10",
         "parameters":{  

         },
         "resultDataContents":[  
            "rest"
         ],
         "includeStats":true
      }    ] }

更多信息:

我已经尝试将 Widget 用作 @QueryResult 和 @NodeEntity(带 getter 和 setter)。

@QueryResult
public class TypeaheadData {
  public Object id;
  public String description;
  public String uid;
}

@NodeEntity
public class TypeaheadData {
  public Object id;
  public String description;
  public String uid;

  public TypeaheadData() {
  }

  public Object getId() {
    return id;
  }

  public void setId(Object id) {
    this.id = id;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public String getUid() {
    return uid;
  }

  public void setUid(String uid) {
    this.uid = uid;
  }
}

我还检查了线路上的响应,在这两种情况下它看起来像这样:

{  
   "results":[  
      {  
         "columns":[  
            "id",
            "description",
            "uid"
         ],
         "data":[  
            {  
               "graph":{  
                  "nodes":[  

                  ],
                  "relationships":[  

                  ]
               }
            },
            {  
               "graph":{  
                  "nodes":[  

                  ],
                  "relationships":[  

                  ]
               }
            },
            {  
               "graph":{  
                  "nodes":[  

                  ],
                  "relationships":[  

                  ]
               }
            },
            {  
               "graph":{  
                  "nodes":[  

                  ],
                  "relationships":[  

                  ]
               }
            },
            {  
               "graph":{  
                  "nodes":[  

                  ],
                  "relationships":[  

                  ]
               }
            },
            {  
               "graph":{  
                  "nodes":[  

                  ],
                  "relationships":[  

                  ]
               }
            },
            {  
               "graph":{  
                  "nodes":[  

                  ],
                  "relationships":[  

                  ]
               }
            },
            {  
               "graph":{  
                  "nodes":[  

                  ],
                  "relationships":[  

                  ]
               }
            },
            {  
               "graph":{  
                  "nodes":[  

                  ],
                  "relationships":[  

                  ]
               }
            },
            {  
               "graph":{  
                  "nodes":[  

                  ],
                  "relationships":[  

                  ]
               }
            }
         ]
      }
   ],
   "errors":[  

   ]
}

如果我删除 Widget @NodeEntity,这是发出的请求:

{  
   "statements":[  
      {  
         "statement":"MATCH (n:Widget) WHERE (n.partNumber STARTS WITH {queryString}) RETURN n.partNumber AS id, n.name AS description, n.urn AS urn LIMIT {limit}",
         "parameters":{  
            "queryString":"001",
            "limit":10
         },
         "resultDataContents":[  
            "row"
         ],
         "includeStats":false
      }
   ]
}

并且删除了 Widget @NodeEntity,响应中确实有正确的数据,但是映射器抛出:

Scalar response queries must only return one column. Make sure your cypher query only returns one item.

OGM 无法将属性集合映射到域实体。 您的查询 returns:

RETURN n.partNumber AS id, n.name AS description, n.urn AS urn 

但是没有什么可以告诉 OGM 这是一个什么样的实体,如果它是一个的话。

将其更改为 RETURN n 应该可以用 neo4jOperations.queryForObjects(Widget.class, query, params);

完成工作

Neo4j OGM 无法处理不 return 整个节点对象的映射查询。如果您在查询中仅请求节点属性的子集,则必须使用 returns Result 的查询方法。然后你必须自己做映射。

如果您使用的是 spring-data-neo4j,那么您可以使用他们的 @QueryResult 注释与存储库 @Query 混合来为您处理映射。如果您查看代码,就会发现他们从 Neo4jSession 提供的元数据中骗取了一个映射器。

一个例外是,如果您查询节点上的单个属性,则 queryForObjects 函数将起作用。

这对我来说似乎是一个疏忽,但我能说谁呢。