“Cannot extract single value from Iterable” with c

2019-08-11 11:22发布

问题:

I'm using SpringDataNeo4J 3.0.0-M1 with Neo4J 2.00-M0.6.

I have BetDAO nodes, linked to a collection of BetAnswerDAO nodes with BET_ANSWER relationship.

I'm trying to retrieve a bet and all its answers on the same request. I have a BetRepository with the following request :

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

My BetWithInfo object :

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

The request is executed without errors, but when I try to access the bet answers collections, I get the following error :

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

It seems to me that what I've done is really close from the example in spring-data documentation. What am I doing wrong ?

EDIT : I made a mistake in my code. I get this error when the getBetAnswers() function returns an Iterable. With a Collection, I get the following error :

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

回答1:

Are you sure

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

returns only 1 result?

From what I understand, this query returns several rows at runtime but Spring Data is told to return only 1 BetWithInfo.

Either double-check your query thus making sure it returns only 1 row, or migrate your method return type from BetWithInfo to Iterable<BetWithInfo> (or Collection<BetWithInfo>).



回答2:

In the BetWithInfo interface, try changing

Collection<BetAnswerDAO> getBetAnswers();

to

Iterable<BetAnswerDAO> getBetAnswers();


回答3:

I looks like it is a bug as described here google forum. that will be fixed on the next spring-data-neo4j release. It is still present in spring-data-neo4j 3.3.0.RELEASE.