Announcement Announcement Module
Collapse
No announcement yet.
Spring Data Neo4j Cypher query IS NULL problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Data Neo4j Cypher query IS NULL problem

    I have the following Domain class that is stored in an embedded Neo4j database:
    (Spring 4.0.5.RELEASE, SDN 3.1.0.RELEASE, Neo4j 2.0.3)

    Code:
    @TypeAlias("TreeNode")
    public class Neo4jNode extends AbstractGraphEntity implements TreeNode<Neo4jNode>
    {
        @Indexed
        private String name;
    
        @Fetch
        @RelatedTo(type = "IS_PARENT_OF", direction = Direction.INCOMING)
        private Neo4jNode parent = null;
    
        @RelatedTo(type = "IS_PARENT_OF")
        private Set<Neo4jNode> children = null;
    ...
        public Neo4jNode addChild(final Neo4jNode node) {
            if (null == children) {
                children = Sets.newHashSet();
            }
            node.setParent(this);
            children.add(node);
            return this;
        }
    }
    My repository contains the following method, along others, to find the root node of the tree structure, that is the node where parent == null:

    Code:
    public interface TreeNodeRepository extends GraphRepository<Neo4jNode>
    {
         @Query("MATCH (n:TreeNode) WHERE n.parent IS NULL RETURN n")
         //@Query("MATCH (n:TreeNode) WHERE NOT (HAS (n.parent)) RETURN n")
        Neo4jNode findRootNode();
    }
    The following test code uses the repository:

    Code:
    @Test
    @Transactional
    public void testFindByRootNode() {
        Neo4jNode root = createNode("root");
        final Neo4jNode node_1 = createNode("node_1");
        root = root.addChild(node_1);
        root = repository.save(root);
        Neo4jNode result = repository.findRootNode();
        assertThat(result, is(root));
    }
    But this test alwasy fails withe the following exception:
    Code:
    java.util.NoSuchElementException: More than one element in [email protected]06e40ac. First element is 'TreeNode[nodeId=0, uuid=a904dc8a603a4c71bd2c24f9fbe73988, name=root, parent=null, children=1]' and the second element is 'TreeNode[nodeId=1, uuid=b333565941c44effba03bea862e3db70, name=node_1, parent=root, children=0]'
    This means, the Query in my repository is wrong, because both nodes are in the result list, but I don't know why!
    I tried both queries with the same result by the way...
    The addChild-method in my node works correctly, parent is set and the child note is added to the parent node, checked that with debugger.
    In my opinion this is the correct way according to the Neo4j documentation, but it seems not.

    Any ideas????
    Thanks!

    Markus



  • #2
    Solved:

    @Query("MATCH n WHERE NOT ()-[:IS_PARENT_OF]->(n:TreeNode) RETURN n")
    Neo4jNode findRootNode();

    Comment


    • #3
      Please, move your question to the StackOverflow - we are going to close this forum soon and rely on SO.
      We need to clean this forum (old unanswered question) before close it.

      Thanks for understanding

      Comment

      Working...
      X