Announcement Announcement Module
Collapse
No announcement yet.
Prent-child model? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Prent-child model?

    I am trying to model a bi-direction one-to-many relationship (a tree). A node can have a parent, and has a children collection. This is straigh-forward in most persistance framewroks. But I can not find the 'correct/best' way to do it with neo4j. I read that when traversing, direction is ignored, so the 'parent' relationship does not need to exist. But in my Java code, something should exist to show this relationship.

    I've spent an hour or so searching around, and can't find this simple example. I'd appreciate any pointers.

    Thanks.
    Last edited by treaves; Jul 19th, 2012, 11:38 AM. Reason: type in title

  • #2
    treaves,

    I assume you are using SDN - have you tried annotating with Direction.BOTH? http://spring.neo4j.org/docs#d0e764

    Direction is not ignored in traversels as such - we mean to say, you can traverse in both directions even if a single relationship only has one direction.

    Regards,

    Lasse

    Comment


    • #3
      Thanks for the response.

      That doesn't do it though. If I have a class thus:

      Code:
      public class Element {
        Set<Element> children;
        Element parent;
        Set<Foo> foos;
      }
      and I want to find an instance of Foo foo, I could do something like:

      Code:
      public Foo findFoo(String fooName) {
        Foo foo = null;
        foreach(Foo testFoo : foos){
          if (testFoo.name == fooName) {
            foo = testFoo;
            break;
          }
        }
        if(foo == null && parent != null){
          foo = parent.findFoo(fooName);
        }
        return foo;
      }
      But if I had instead:
      Code:
      public class Element {
        @RelatedTo(type = "CHILDREN", direction=Direction.BOTH)
        Set<Element> children;
        Set<Foo> foos;
      }
      it doesn't work the same way.

      So rewording my question, is the first way I show here the better way of implemnting it?
      Last edited by treaves; Jul 20th, 2012, 08:03 AM. Reason: Code format

      Comment


      • #4
        treaves,

        Gotcha. I think this should do it:

        Code:
        Public class Person {
            // defaults to Direction.OUTGOING
            @RelatedTo(type="PARENT_OF")
            Set<Person> children;
        
            @RelatedTo(type="PARENT_OF", direction=Direction.INCOMING)
            Person parent;
        
            //or, more anatomically correct,
            //@RelatedTo(type="PARENT_OF", direction=Direction.INCOMING)
            //Set<Person> parents;
        }
        Does that help?

        Lasse

        Comment


        • #5
          Yes, it does, thanks. In my case, parent is singular, but, yes.

          Comment

          Working...
          X