2013-12-20 303 views
1

我使用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時出現此錯誤。隨着收藏,我得到以下錯誤:

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

回答

0

在BetWithInfo界面,嘗試改變

Collection<BetAnswerDAO> getBetAnswers(); 

Iterable<BetAnswerDAO> getBetAnswers(); 
+0

我得到與Iterable相同的錯誤。 –

+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數據被告知只返回1個BetWithInfo。

請仔細檢查您的查詢,以確保它只返回1行,或將您的方法返回類型從BetWithInfo遷移到Iterable<BetWithInfo>(或Collection<BetWithInfo>)。

+0

該請求在Neo4j瀏覽器中只返回一行。 如果我將我的方法返回類型更改爲Iterable,我得到一個包含一個BetWithInfo對象的列表,並在調用getBetAnswers()時收到同樣的錯誤。 我沒有問題調用getBet(),錯誤只發生在試圖訪問Iterable/Collection時。 –

+0

我想如果你顯示你手動執行的查詢的結果會有所幫助。 – Rolf

0

我看起來像這裏描述的一個bug,如google forum所述。這將在下一個spring-data-neo4j版本中得到修復。 它仍然存在於spring-data-neo4j 3.3.0.RELEASE