“无法从自定义查询结果对象中提取Iterable中的单个值”

时间:2013-12-20 14:26:13

标签: spring neo4j spring-data-neo4j

我正在使用 SpringDataNeo4J 3.0.0-M1 Neo4J 2.00-M0.6

我有BetDAO节点,链接到具有BET_ANSWER关系的BetAnswerDAO节点集合。

我正在尝试在同一请求中检索赌注及其所有答案。我有一个BetRepository,其中包含以下请求:

@Query("start bet=node:BetDAO(id = {betId}) "
    + "match (bet)-[:BET_ANSWER]->(betAnswer) "
    + "return bet, collect(betAnswer) as betAnswers")
BetWithInfo getByIdWithInfo(@Param("betId") String id);

我的BetWithInfo对象:

import org.springframework.data.neo4j.annotation.QueryResult;
import org.springframework.data.neo4j.annotation.ResultColumn;

import fr.project.commons.data.graph.neo4j.beans.nodes.BetAnswerDAO;
import fr.project.commons.data.graph.neo4j.beans.nodes.BetDAO;

@QueryResult
public interface BetWithInfo {

    @ResultColumn("bet")
    BetDAO getBet();

    @ResultColumn("betAnswers")
    Iterable<BetAnswerDAO> getBetAnswers();
}

请求执行没有错误,但是当我尝试访问投注答案集合时,我收到以下错误:

Caused by: java.lang.RuntimeException: Cannot extract single value from Iterable with more than one elements.
    at org.springframework.data.neo4j.conversion.DefaultConverter.extractSingle(DefaultConverter.java:60)
    at org.springframework.data.neo4j.conversion.DefaultConverter.extractValue(DefaultConverter.java:51)
    at org.springframework.data.neo4j.conversion.DefaultConverter.convert(DefaultConverter.java:40)
    at org.springframework.data.neo4j.support.conversion.EntityResultConverter.convert(EntityResultConverter.java:165)
    at org.springframework.data.neo4j.conversion.QueryResultBuilder$1.convert(QueryResultBuilder.java:103)
    at org.springframework.data.neo4j.conversion.QueryResultBuilder$1.access$300(QueryResultBuilder.java:81)
    at org.springframework.data.neo4j.conversion.QueryResultBuilder$1$1.underlyingObjectToObject(QueryResultBuilder.java:121)
    at org.neo4j.helpers.collection.IteratorWrapper.next(IteratorWrapper.java:47)
    at org.neo4j.helpers.collection.IteratorUtil.addToCollection(IteratorUtil.java:403)
    at org.neo4j.helpers.collection.IteratorUtil.addToCollection(IteratorUtil.java:482)
    at org.neo4j.helpers.collection.IteratorUtil.asCollection(IteratorUtil.java:581)
    at org.springframework.data.neo4j.support.conversion.EntityResultConverter.extractAndSetValueOfField(EntityResultConverter.java:129)
    at org.springframework.data.neo4j.support.conversion.EntityResultConverter.extractPOJOResult(EntityResultConverter.java:102)
    at org.springframework.data.neo4j.support.conversion.EntityResultConverter.convert(EntityResultConverter.java:163)
    at org.springframework.data.neo4j.conversion.DefaultConverter.convert(DefaultConverter.java:36)
    at org.springframework.data.neo4j.rest.SpringRestGraphDatabase$SpringResultConverter.convert(SpringRestGraphDatabase.java:148)
    at org.neo4j.rest.graphdb.util.QueryResultBuilder$1$1.underlyingObjectToObject(QueryResultBuilder.java:98)
    at org.neo4j.helpers.collection.IteratorWrapper.next(IteratorWrapper.java:47)
    at org.neo4j.helpers.collection.IteratorUtil.single(IteratorUtil.java:334)
    at org.neo4j.helpers.collection.IteratorUtil.singleOrNull(IteratorUtil.java:130)
    at org.neo4j.helpers.collection.IteratorUtil.singleOrNull(IteratorUtil.java:287)
    at org.springframework.data.neo4j.rest.SpringEndResult.singleOrNull(SpringEndResult.java:39)
    at org.springframework.data.neo4j.repository.query.GraphRepositoryQuery.dispatchQuery(GraphRepositoryQuery.java:115)
    at org.springframework.data.neo4j.repository.query.GraphRepositoryQuery$1.doWithGraph(GraphRepositoryQuery.java:86)
    at org.springframework.data.neo4j.support.Neo4jTemplate.doExecute(Neo4jTemplate.java:408)
    at org.springframework.data.neo4j.support.Neo4jTemplate.access$000(Neo4jTemplate.java:85)
    at org.springframework.data.neo4j.support.Neo4jTemplate$2.doInTransaction(Neo4jTemplate.java:422)
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:130)
    at org.springframework.data.neo4j.support.Neo4jTemplate.exec(Neo4jTemplate.java:419)
    at org.springframework.data.neo4j.repository.query.GraphRepositoryQuery.execute(GraphRepositoryQuery.java:80)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:337)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
    ... 59 more

在我看来,我所做的与spring-data documentation中的例子非常接近。我做错了什么?

编辑: 我在代码中犯了一个错误。当getBetAnswers()函数返回Iterable时,我收到此错误。使用Collection,我收到以下错误:

java.lang.ClassCastException: org.springframework.data.neo4j.conversion.QueryResultBuilder$1 cannot be cast to java.util.Collection

3 个答案:

答案 0 :(得分:1)

你确定

@Query("start bet=node:BetDAO(id = {betId}) "
+ "match (bet)-[:BET_ANSWER]->(betAnswer) "
+ "return bet, collect(betAnswer) as betAnswers")
BetWithInfo getByIdWithInfo(@Param("betId") String id);

只返回1个结果?

根据我的理解,此查询在运行时返回多行,但Spring Data被告知只返回1个BetWithInfo。

仔细检查您的查询,确保只返回1行,或将方法返回类型从BetWithInfo迁移到Iterable<BetWithInfo>(或Collection<BetWithInfo>)。

答案 1 :(得分:0)

在BetWithInfo界面中,尝试更改

Collection<BetAnswerDAO> getBetAnswers();

Iterable<BetAnswerDAO> getBetAnswers();

答案 2 :(得分:0)

我看起来像这里描述的错误google forum。这将在下一个spring-data-neo4j版本中修复。 它仍然存在于spring-data-neo4j 3.3.0.RELEASE