Neo4J:无法从具有多个元素的Iterable中提取单个值

时间:2015-01-25 22:09:41

标签: java spring neo4j

我是Spring的新手,并尝试使用Neo4j开始为开源Webstore构建API。现在,我遇到了从存储库中的自定义@ Query&#获取正确响应的问题。

这是回复:

{
  "timestamp": 1422223603656,
  "status": 500,
  "error": "Internal Server Error",
  "exception": "java.lang.RuntimeException",
  "message": "Cannot extract single value from Iterable with more than one elements.",
  "path": "/api/v1/articles"
}

以下是查询:

@Override
@Query("MATCH (a:`Article`)-[r:CATEGORIZED_TO]->() RETURN a.name as name, a.number as number, r.segment as segment")
Result<Article> findAll();

我以这种方式使用存储库:

@RequestMapping(value = "/articles", method = RequestMethod.GET)
public
@ResponseBody
Iterable<Article> list(
        @RequestParam(value = "segment", required = false) String segment
) {
    Iterable<Article> articles = null;

    Transaction tx = graphDatabase.beginTx();

    try {
        Result result = articleRepository.findAll();

        articles = result.as(Article.class);

        tx.success();
    } finally {
        tx.close();
    }

    return articles;
}

你能发现问题吗?

编辑:

这是文章类

@NodeEntity
public class Article {

  @GraphId
  private Long graphId;

  @GraphProperty
  private String name;

  @Indexed(unique = true)
  private String number;

  @RelatedTo(elementClass = Category.class, type = "BELONGS_TO", direction = Direction.OUTGOING)
  Set<Category> categories;

  @RelatedToVia(elementClass = Listing.class, type = "BELONGS_TO", direction = Direction.OUTGOING)
  Iterable<Listing> listings;

  public Article() {
  }

  public Article(String name, String number) {
    this.name = name;
    this.number = number;
  }

  public Long getGraphId() {
    return graphId;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getNumber() {
    return number;
  }

  public void setNumber(String number) {
    this.number = number;
  }

  public Collection<Category> getCategories() {
    return categories;
  }

  public Collection<Listing> getListings() {
    return IteratorUtil.asCollection(listings);
  }

}

2 个答案:

答案 0 :(得分:1)

您没有分享Article课程的定义。

使用带有@QueryResult注释的DTO课程,或者Article是您的域类,您必须return a

Result DSL上的正确方法是.to(Article.class)而不是as(),后者用于投放到集合。

答案 1 :(得分:0)

我遇到了和你一样的问题。只需尝试在查询中仅返回“a”,它就会起作用!祝你好运