2014-04-16 133 views
4

我做了一個簡單的存儲庫,其中包含我係統中的所有人員,一切似乎都正常工作。我可以根據給定的鍵和值添加,刪除甚至檢索單個人,但由於某些原因,我無法直接檢索存儲在存儲庫中的所有人員。我試圖通過使用findAll()方法來實現,它應該返回一個可迭代的。然後我嘗試迭代迭代器並將每個人存儲在我想要函數返回的ArrayList中。Spring Data Neo4J存儲庫findAll()導致nullpointerexception

public List<Person> getAllPersons() { 
    Iterable<Person> p = personRepository.findAll(); 
    System.out.println("person count: " + personRepository.count()); 
    List<Person> persons = new ArrayList<Person>(); 
    for (Person person : p) { 
     System.out.println("person"); 
     persons.add(person); 
    } 
    return persons; 
} 

打印行打印「3」,以便存儲庫不爲空,但仍然在下面的錯誤中得到nullpointerexception。

apr 16, 2014 3:45:54 PM org.apache.catalina.core.StandardWrapperValve invoke 
SEVERE: Servlet.service() for servlet [bmsapp] in context with path [/bmsapp] threw exception [Request processing failed; nested exception is org.neo4j.graphdb.TransactionFailureException: Failed to mark transaction as rollback only.] with root cause 
java.lang.NullPointerException 
    at org.neo4j.kernel.TopLevelTransaction.markAsRollbackOnly(TopLevelTransaction.java:93) 
    at org.neo4j.kernel.TopLevelTransaction.failure(TopLevelTransaction.java:86) 
    at org.neo4j.cypher.internal.spi.v2_0.TransactionBoundExecutionContext.close(TransactionBoundExecutionContext.scala:58) 
    at org.neo4j.cypher.internal.compiler.v2_0.spi.DelegatingQueryContext.close(DelegatingQueryContext.scala:32) 
    at org.neo4j.cypher.internal.compiler.v2_0.spi.ExceptionTranslatingQueryContext.org$neo4j$cypher$internal$compiler$v2_0$spi$ExceptionTranslatingQueryContext$$super$close(ExceptionTranslatingQueryContext.scala:34) 
    at org.neo4j.cypher.internal.compiler.v2_0.spi.ExceptionTranslatingQueryContext$$anonfun$close$1.apply$mcV$sp(ExceptionTranslatingQueryContext.scala:34) 
    at org.neo4j.cypher.internal.compiler.v2_0.spi.ExceptionTranslatingQueryContext$$anonfun$close$1.apply(ExceptionTranslatingQueryContext.scala:34) 
    at org.neo4j.cypher.internal.compiler.v2_0.spi.ExceptionTranslatingQueryContext$$anonfun$close$1.apply(ExceptionTranslatingQueryContext.scala:34) 
    at org.neo4j.cypher.internal.compiler.v2_0.spi.ExceptionTranslatingQueryContext.org$neo4j$cypher$internal$compiler$v2_0$spi$ExceptionTranslatingQueryContext$$translateException(ExceptionTranslatingQueryContext.scala:149) 
    at org.neo4j.cypher.internal.compiler.v2_0.spi.ExceptionTranslatingQueryContext.close(ExceptionTranslatingQueryContext.scala:34) 
    at org.neo4j.cypher.internal.compiler.v2_0.ClosingIterator.failIfThrows(ClosingIterator.scala:94) 
    at org.neo4j.cypher.internal.compiler.v2_0.ClosingIterator.next(ClosingIterator.scala:45) 
    at org.neo4j.cypher.internal.compiler.v2_0.PipeExecutionResult.next(PipeExecutionResult.scala:168) 
    at org.neo4j.cypher.internal.compiler.v2_0.PipeExecutionResult.next(PipeExecutionResult.scala:34) 
    at scala.collection.Iterator$$anon$11.next(Iterator.scala:328) 
    at scala.collection.convert.Wrappers$IteratorWrapper.next(Wrappers.scala:30) 
    at org.neo4j.cypher.internal.compiler.v2_0.PipeExecutionResult$$anon$1.next(PipeExecutionResult.scala:76) 
    at org.neo4j.helpers.collection.IteratorWrapper.next(IteratorWrapper.java:47) 
    at org.neo4j.helpers.collection.IteratorWrapper.next(IteratorWrapper.java:47) 
    at bmsapp.repository.NeoPersonDao.getAllPersons(NeoPersonDao.java:33) 
    at bmsapp.service.SimplePersonManager.getPersons(SimplePersonManager.java:32) 
    at bmsapp.web.HelloController.HelloHandler(HelloController.java:45) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:483) 
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175) 
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446) 
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961) 
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at java.lang.Thread.run(Thread.java:744) 

我已經浪費了幾個小時,以解決這個問題,但沒有任何運氣。任何人都可以幫我在這裏嗎?

+0

也許你應該使用'Iterator it = p.iterator()' – remigio

+0

我已經嘗試了Iterator和Iterable選項,但是一旦我嘗試訪問它們,都會導致空指針異常。 – Jonhy

回答

1

感謝您的答案!我設法解決問題,通過添加

"<tx:annotation-driven mode="proxy"/> 

我的應用程序上下文xml文件。它現在工作完美!然而,我仍然不確定這個語句是什麼,爲什麼我只需要findAll()方法,而不是其他任何我用過的。 (count(),findOne(),findBySchemaPropertyValue()等)

+0

我不明白這是怎麼產生的影響,mode =「proxy」vs mode =「aspectj」只有在你調用非代理對象的方法時纔有意義。更多信息[這裏](http://stackoverflow.com/a/4162325/1000455)。 – ChoppyTheLumberjack

2

請嘗試將您的personRepository.findAll()設爲@Transactional

如果它仍然不起作用,你可以發佈你的personRepository.findAll()方法嗎?

+0

謝謝。也許我應該顯示更多我的代碼。但是我已經用@transactional註解標記了方法。 personRepository.findAll()也是標準的neo4j模板findAll()函數。 – Jonhy

+0

也許你應該在將來粘貼更多的代碼,因爲我的答案會和@Conan一樣。 – lulu88

+0

如果這是你的建議,它不能解決問題。 '公共接口FooRepository擴展GraphRepository { \t @Override @Transactional結果 findAll(); }' –

0

我試過模式=「代理」,它不適合我,這應該只有一個影響,如果你使用非注入事務方法互相打電話(說明here)。

此'TransactionFailureException'可能與問題相關:https://jira.spring.io/browse/DATAGRAPH-531。如果是這樣的話,你需要改變

service.findAll() 

到:

service.findAll().as(Collection.class) 

...和它應該消失。

相關問題