Spring Data Neo4J存储库findAll()导致nullpointerexception

时间:2014-04-16 14:07:53

标签: java spring spring-data spring-data-neo4j

我制作了一个包含系统中所有人的简单存储库,一切似乎都运行良好。我可以根据给定的密钥和值添加,删除甚至检索单个人,但由于某种原因,我无法直接检索存储在存储库中的所有人。我尝试使用findAll()方法,它应该返回一个iterable。然后我尝试迭代迭代并将每个人存储在一个arraylist中,我希望函数返回。

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

打印线打印&#34; 3&#34;所以存储库不是空的,但我仍然在下面的错误中得到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)

为了解决这个问题,我已经浪费了几个小时但没有运气。有人可以帮帮我吗?

3 个答案:

答案 0 :(得分:2)

尝试将personRepository.findAll()标记为@Transactional

如果它仍然不起作用,您可以发布personRepository.findAll()方法PLZ吗?

答案 1 :(得分:1)

感谢您的回答!我已设法通过添加

来解决问题
"<tx:annotation-driven mode="proxy"/>

到我的应用程序上下文xml文件。它现在工作得很好!但是,我仍然不确定这个状态是什么以及为什么我只需要它用于findAll()方法,而不是我使用过的任何其他方法。 (count(),findOne(),findBySchemaPropertyValue()等)

答案 2 :(得分:0)

我尝试了mode =“proxy”并且它对我不起作用,如果您使用非注入的事务方法相互调用,这应该只会产生影响(解释here)。

这个'TransactionFailureException'可能与问题有关:https://jira.spring.io/browse/DATAGRAPH-531。如果是这种情况,则需要更改

service.findAll()

为:

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

......它应该消失。