我是 Spring 新手,正在尝试开始使用 Neo4j 为开源网上商店构建 API。现在,我在从存储库中的自定义 @Query'ies 获取正确响应时遇到问题。
这是回复:
{
"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;
}
你能发现问题吗?
编辑:
这是 Article 类
@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);
}
}
最佳答案
您没有共享 Article
类的定义。
使用带有 @QueryResult
注释的 DTO 类,或者如果 Article
是您的域类,则必须返回
此外,Result
DSL 上的正确方法是 .to(Article.class)
而不是 as()
后者用于投影到 Collection 。
关于java - Neo4J:无法从具有多个元素的 Iterable 中提取单个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28142146/