I am currently working on spring-data-neo4j and need to get the children of certain nodes.
Here is the domain
@NodeEntity
public class GLNode {
@GraphId
private Long id;
@Property(name="description")
private ArrayList<String> desc;
public ArrayList<String> getDesc() {
return this.desc;
}
@Relationship(type = "GLRelationship")
private Collection<GLNode> children;
public Collection<GLNode> getChildren(){
return this.children;
}
}
here is the repository
@Query("MATCH (n:GLNode) WHERE id(n)={0} RETURN n")
GLNode getGLNodeFromId(Integer idOfNode);
In the service, I successfully get the node but get null pointer when try to access its children like:
GLNode m = repo.getGLNodeFromId(0);
System.out.println(m.getChildren().iterator().next().getDesc().toString());
Here's the exception:
java.lang.NullPointerException
com.hersbitcloud.cancercloud.services.GLService.getGLNodes(GLService.java:40)
com.hersbitcloud.cancercloud.controllers.GLController.getGLNodes(GLController.java:36)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:497)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.hersbitcloud.cancercloud.CORSFilter.doFilter(CORSFilter.java:24)
org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:87)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
I am sure m
has been successfully retrieved. What am I doing wrong here?
Thank you.
getChildren()
will be null with your custom query
@Query("MATCH (n:GLNode) WHERE id(n)={0} RETURN n")
GLNode getGLNodeFromId(Integer idOfNode);
because the query returns only the node and no relationships (depth 0).
If you are retrieving the node by ID, then repository.findOne(id)
or neo4jOperations.load(GLNode.class,id)
would be more appropriate. These methods load entities to a default depth of 1 which means, related entities one hop away.
You can change this default depth using repository.findOne(id,depth)
or neo4jOperations.load(GLNode.class,id,depth)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.