Results 1 to 5 of 5

Thread: Prent-child model?

  1. #1
    Join Date
    Jun 2012
    Posts
    18

    Default Parent-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 at 11:38 AM. Reason: type in title

  2. #2
    Join Date
    May 2012
    Posts
    107

    Default

    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

  3. #3
    Join Date
    Jun 2012
    Posts
    18

    Default

    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 at 08:03 AM. Reason: Code format

  4. #4
    Join Date
    May 2012
    Posts
    107

    Default

    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

  5. #5
    Join Date
    Jun 2012
    Posts
    18

    Default

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •