Spring data wth ne04j error…error while retrieving

2020-04-21 00:09发布

问题:

I was using spring data with neo4j.

I used @Query annotation to place the query in the repository, the query was as follows

@Query(value = "START me=node({0}), friend=node({1}) "
            + "MATCH p=shortestPath(me-[:ACTIVEFRIEND*..]->friend)"
            + " RETURN p")

public Iterable<EntityPath<User, User>> getShortestPathBetween(User a, User b);

In the controller I am accessing it as

 Iterable<EntityPath<User, User>> shortestPathBetween = this.queryService.getShortestPathBetween(user, friend);
        for (EntityPath<User, User> path : shortestPathBetween) {
            Iterator<User> iter = path.<User>nodeEntities().iterator();
        }

When it tries to access path.nodeEntities, it is causing this error:

nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at org.springframework.data.neo4j.support.path.ConvertingEntityPath.nodes(ConvertingEntityPath.java:137)
at org.springframework.data.neo4j.support.path.ConvertingEntityPath.nodeEntities(ConvertingEntityPath.java:69)
at com.laindain.cms.controller.QueryController.getShortestPathBetween(QueryController.java:194)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789) at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) 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.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1822) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) at java.lang.Thread.run(Thread.java:722)

I will appreciate if someone can help me on this.

Thanks Hemant

回答1:

Try defining the method this way:

public EndResult<EntityPath<User, User>> getShortestPathBetween(User a, User b);

since the query returns a path and not a collection of paths. You can get the result as follows:

EntityPath<User, User> shortestPathBetween =
this.queryService.getShortestPathBetween(user, friend).to(EntityPath);

The nodes and relationships on the path can then be accessed using the nodeEntities() and the relationshipEntities() methods on the returned EntityPath.