我正在尝试使用Spring-Data-Neo4j , Version = 3.2.1.RELEASE
。当我使用存储库findAll()
方法获取节点时,我得到了例外:
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_1.TransactionBoundQueryContext.close(TransactionBoundQueryContext.scala:65)
at org.neo4j.cypher.internal.compiler.v2_1.spi.DelegatingQueryContext.close(DelegatingQueryContext.scala:38)
at org.neo4j.cypher.internal.compiler.v2_1.spi.ExceptionTranslatingQueryContext.org$neo4j$cypher$internal$compiler$v2_1$spi$ExceptionTranslatingQueryContext$$super$close(ExceptionTranslatingQueryContext.scala:34)
at org.neo4j.cypher.internal.compiler.v2_1.spi.ExceptionTranslatingQueryContext$$anonfun$close$1.apply$mcV$sp(ExceptionTranslatingQueryContext.scala:34)
at org.neo4j.cypher.internal.compiler.v2_1.spi.ExceptionTranslatingQueryContext$$anonfun$close$1.apply(ExceptionTranslatingQueryContext.scala:34)
at org.neo4j.cypher.internal.compiler.v2_1.spi.ExceptionTranslatingQueryContext$$anonfun$close$1.apply(ExceptionTranslatingQueryContext.scala:34)
at org.neo4j.cypher.internal.compiler.v2_1.spi.ExceptionTranslatingQueryContext.org$neo4j$cypher$internal$compiler$v2_1$spi$ExceptionTranslatingQueryContext$$translateException(ExceptionTranslatingQueryContext.scala:152)
at org.neo4j.cypher.internal.compiler.v2_1.spi.ExceptionTranslatingQueryContext.close(ExceptionTranslatingQueryContext.scala:34)
at org.neo4j.cypher.internal.compiler.v2_1.spi.DelegatingQueryContext.close(DelegatingQueryContext.scala:38)
at org.neo4j.cypher.internal.compiler.v2_1.executionplan.ExecutionWorkflowBuilder$$anonfun$runWithQueryState$1.apply(ExecutionPlanBuilder.scala:164)
at org.neo4j.cypher.internal.compiler.v2_1.executionplan.ExecutionWorkflowBuilder$$anonfun$runWithQueryState$1.apply(ExecutionPlanBuilder.scala:164)
at org.neo4j.cypher.internal.compiler.v2_1.TaskCloser$$anonfun$1.apply(TaskCloser.scala:43)
at org.neo4j.cypher.internal.compiler.v2_1.TaskCloser$$anonfun$1.apply(TaskCloser.scala:41)
at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:251)
at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:251)
at scala.collection.immutable.List.foreach(List.scala:318)
at scala.collection.TraversableLike$class.flatMap(TraversableLike.scala:251)
at scala.collection.AbstractTraversable.flatMap(Traversable.scala:105)
at org.neo4j.cypher.internal.compiler.v2_1.TaskCloser.close(TaskCloser.scala:40)
at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator$$anonfun$close$1.apply$mcV$sp(ClosingIterator.scala:67)
at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator$$anonfun$close$1.apply(ClosingIterator.scala:67)
at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator$$anonfun$close$1.apply(ClosingIterator.scala:67)
at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator$$anonfun$translateException$1.apply(ClosingIterator.scala:72)
at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator.decoratedCypherException(ClosingIterator.scala:102)
at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator.translateException(ClosingIterator.scala:70)
at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator.close(ClosingIterator.scala:66)
at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator$$anonfun$failIfThrows$1.apply(ClosingIterator.scala:96)
at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator.decoratedCypherException(ClosingIterator.scala:102)
at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator.failIfThrows(ClosingIterator.scala:91)
at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator.next(ClosingIterator.scala:44)
at org.neo4j.cypher.internal.compiler.v2_1.PipeExecutionResult.next(PipeExecutionResult.scala:169)
at org.neo4j.cypher.internal.compiler.v2_1.PipeExecutionResult.next(PipeExecutionResult.scala:35)
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_1.PipeExecutionResult$$anon$1.next(PipeExecutionResult.scala:77)
at org.neo4j.helpers.collection.IteratorWrapper.next(IteratorWrapper.java:47)
at org.neo4j.helpers.collection.IteratorWrapper.next(IteratorWrapper.java:47)
at java.lang.Iterable.forEach(Iterable.java:74)
at com.harmeetsingh13.service.impl.PersonServiceImpl.getAllPersons(PersonServiceImpl.java:48)
at com.harmeetsingh13.controller.PersonController.makeFriends(PersonController.java:55)
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.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
此例外与交易有关。当我在google上搜索时,有很多关于此异常的问题,但有些使用@Transactional
anotation来解决问题。我也试图使用这个注释,但仍面临同样的错误。以下是我的代码和配置。
@Service
@Transactional
public class PersonServiceImpl implements PersonService{
@Autowired
private RepositoryPerson repositoryPerson;
@Autowired
private Neo4jTemplate template;
@Override
public Person save(Person entity) {
return repositoryPerson.save(entity);
}
@Override
public Person findPersonByProperty(String property, Object value) {
return repositoryPerson.findBySchemaPropertyValue(property, value);
}
@Override
public List<Person> getAllPersons() {
Result<Person> persons = repositoryPerson.findAll();
List<Person> personsList = new ArrayList<>();
persons.forEach(person -> personsList.add(person));
return personsList;
}
}
配置:
@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages={"com.harmeetsingh13.entities", "com.harmeetsingh13.maintainrelationship"})
@PropertySource(value="classpath:properties/db.properties")
@EnableNeo4jRepositories(basePackages = "com.harmeetsingh13.repository")
public class Neo4jConfig extends Neo4jAspectConfiguration{
@Resource
private Environment env;
public Neo4jConfig() {
setBasePackage("com.harmeetsingh13.entities");
}
@Bean(name="graphDatabaseService")
public GraphDatabaseService getGraphDatabaseService(){
GraphDatabaseFactory databaseService = new GraphDatabaseFactory();
return databaseService.newEmbeddedDatabase(env.getProperty("db.store.directory"));
}
private JtaTransactionManagerFactoryBean neo4jTransactionManagerFactoryBean() throws Exception {
JtaTransactionManagerFactoryBean factoryBean =
new JtaTransactionManagerFactoryBean(getGraphDatabaseService());
return factoryBean;
}
@Override
@Bean(name= {"transactionManager"})
public PlatformTransactionManager neo4jTransactionManager() throws Exception {
ChainedTransactionManager transactionManager = new ChainedTransactionManager(neo4jTransactionManagerFactoryBean().getObject());
return transactionManager;
}
}
当我尝试运行测试用例时,测试用例成功运行。以下是我的测试用例的代码。
@Test
@Transactional
public void findAllNodes(){
Result<Person> persons = actorRepo.findAll();
persons.forEach(person -> System.out.println(person));
}
答案 0 :(得分:1)
请参阅https://jira.spring.io/browse/DATAGRAPH-531
它只发生在嵌入式neo4j数据库中;不是REST变种。
简而言之,确保在将结果对象传递到下一层之前将所有内容转换为List / Collection等
见下面的样本测试方法
try (Transaction graphDBTransaction = graphDatabaseService.beginTx()) {
final Collection collection = personRepository.findAll().as(Collection.class);
final int size = collection.size();
assertThat("Total number of persons in test data-set should be 5!", size, equalTo(5));
graphDBTransaction.success();
}