Neo4j OGM Neo4jSession变量替换查询失败

时间:2016-06-14 23:07:27

标签: neo4j spring-data-neo4j-4 neo4j-ogm

对我来说:

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);

它返回一个空列表。我也尝试过我的实际域名对象:

 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,响应DOES中有正确的数据,但映射器抛出:

  

标量响应查询只能返回一列。确保你的   cypher查询只返回一个项目。

2 个答案:

答案 0 :(得分:2)

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

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

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

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

完成工作

答案 1 :(得分:1)

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

如果您正在使用spring-data-neo4j,那么您可以使用他们的@QueryResult注释与存储库@Query混合来为您处理映射。如果你看一下代码,他们就会从Neo4jSession提供的元数据中找到一个mapper。

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

似乎对我有疏忽,但我该说谁。