Results 1 to 6 of 6

Thread: Neo4j - Problems returning items in a relation

  1. #1
    Join Date
    Jul 2012
    Posts
    5

    Default Neo4j - Problems returning items in a relation

    I'm trying to follow the Cineasts example and have created some nodes, a relationship between them and a test case.

    I currently have two node types Event and Tag and an EventTagRelationship RelationshipEntity.

    Within these I have the following (removed a lot of the code for brevity, tell me if you need more).
    EventNode:

    @RelatedToVia(type="EVENT_TAG")
    Collection<EventTagRelationship> tagRelationships;

    @RelatedTo(type="EVENT_TAG")
    Set<TagNode> tags;
    public EventTagRelationship addTag(TagNode tag) {
    final EventTagRelationship tagRel = new EventTagRelationship(tag, this);
    tagRelationships.add(tagRel);
    return tagRel;
    }

    public Set<TagNode> getTags() {
    return tags;
    }

    TagNode:
    @RelatedTo(type = "EVENT_TAG", direction = Direction.INCOMING)
    Set<EventNode> events;

    EventTagRelationship:
    @GraphId Long id;
    @EndNode TagNode tag;
    @StartNode EventNode event;

    And here's my test:
    @Test
    public void eventCanHaveTags() {
    EventNode event = template.save(new EventNode("1", "Super Duper Test Event"));
    TagNode concertTag = template.save(new TagNode("1", "concert"));
    TagNode bandTag = template.save(new TagNode("2", "band"));

    EventTagRelationship eventBandTag = event.addTag(bandTag);
    assertNotNull(eventBandTag);
    template.save(eventBandTag);
    EventTagRelationship eventConcertTag = event.addTag(concertTag);
    assertNotNull(eventConcertTag);
    template.save(eventConcertTag);

    EventNode foundEvent = this.eventRepository.findById("1");
    assertEquals(event, foundEvent);
    assertEquals(event.getTitle(), foundEvent.getTitle());
    assertEquals(2, foundEvent.getTags().size());

    //now let's make sure we can find the tags themselves first
    TagNode foundTag = this.tagRepository.findById("1");

    assertEquals(concertTag.getName(), foundTag.getName());
    System.out.println("number of tags in the event: " + foundEvent.getTags().size());

    //now let's test out our relationships to ensure they are created properly
    TagNode tag1 = foundEvent.getTags().iterator().next();

    <--- where it starts breaking down
    System.out.println("found tag relationship:" + tag1);
    <---
    assertEquals(eventConcertTag, tag1);
    assertEquals(eventConcertTag.getTag().getName(), tag1.getName());
    }

    I can retrieve the event, I can retrieve the tags, but when I try and retrieve the tags related to an event I get a null object. Looking at the size of the tags set it returns 2 as expected, but the first object returns null. Here's the test output:
    number of tags in the event: 2
    found tag relationship:null [null]
    Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 11.147 sec <<< FAILURE!

    Results :

    Failed tests: eventCanHaveTags(com.clearboxmedia.srs.it.graph.Do mainTest): expected:<null [1], concert [1] [1]> but was:<null [null]>

    Like the cineast example I have changed the toString method to output pertinent information like title, name and the [id] of the object.

    Any help is greatly appreciated, thanks!

    -warner

  2. #2
    Join Date
    May 2012
    Posts
    107

    Default

    Quote Originally Posted by warnero View Post
    @RelatedToVia(type="EVENT_TAG")
    Collection<EventTagRelationship> tagRelationships;

    @RelatedTo(type="EVENT_TAG")
    Set<TagNode> tags;
    Warner,

    Could you try changing your relationship type names so they are different?

    When you say type="EVENT_TAG" for both associations, you end up with one overwriting the other.

    If that doesn't work, could you share your EventNode and TagNode classes, so I can have a look. Your code otherwise looks fine and it seems it should just straightforwardly work.

    Lasse

  3. #3
    Join Date
    Jul 2012
    Posts
    5

    Default

    So, originally I just had this:
    @RelatedToVia(type="EVENT_TAG")
    Collection<EventTagRelationship> tagRelationships;

    And then I started to read through the Cineast example further and came across this for Movie:
    https://github.com/SpringSource/spri...ain/Movie.java


    @RelatedTo(type = "ACTS_IN", direction = INCOMING)
    Set<Person> actors;

    @RelatedToVia(elementClass = Role.class, type = "ACTS_IN", direction = INCOMING)
    Iterable<Role> roles;

    So, roles are the actual relationship and the actors are related in the role. Do I need to specify the elementClass? In one of the examples I was looking at it didn't have the elementClass. I'll give that a shot and see if will work now.

  4. #4
    Join Date
    Jul 2012
    Posts
    5

    Default

    Ok, that didn't work . Here's my latest output from the test:

    found tag relationship:null [1], null [null] [1]
    Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 59.066 sec <<< FAILURE!

    Results :

    Failed tests: eventCanHaveTags(com.clearboxmedia.srs.it.graph.Do mainTest): expected:<null [1], concert [1] [1]> but was:<null [null]>

    So, the id's are coming through, but not the event or tag names. Using the same test as before.

    Here's my EventNode and TagNode classes. As well as the relationship.

    EventNode:

    import java.util.Collection;
    import java.util.Set;

    import org.neo4j.graphdb.Direction;
    import org.springframework.data.neo4j.annotation.GraphId;
    import org.springframework.data.neo4j.annotation.GraphPro perty;
    import org.springframework.data.neo4j.annotation.NodeEnti ty;
    import org.springframework.data.neo4j.annotation.RelatedT o;
    import org.springframework.data.neo4j.annotation.Indexed;
    import org.springframework.data.neo4j.annotation.RelatedT oVia;
    import org.springframework.data.neo4j.support.index.Index Type;

    import com.clearboxmedia.srs.domain.Event;

    /**
    * standard event intended for transport with neo4j graph database
    *
    * @author r351574nc3
    */
    @NodeEntity
    public class EventNode {
    @GraphId
    Long nodeId;

    @Indexed(unique=true)
    private String id;

    @Indexed(indexType=IndexType.FULLTEXT, indexName = "event-title")
    private String title;

    @Indexed(indexType=IndexType.FULLTEXT, indexName = "event-description")
    private String description;

    public EventNode() {
    super();
    }

    public EventNode(String id, String description) {
    this.id = id;
    this.description = description;
    }

    @RelatedToVia(type="EVENT_TAG", elementClass=EventTagRelationship.class)
    Collection<EventTagRelationship> tagRelationships;

    //@RelatedTo(type="EVENT_TAG")
    Set<TagNode> tags;

    public String getId() {
    return id;
    }

    public void setId(String id) {
    this.id = id;
    }

    public String getTitle() {
    return title;
    }

    public void setTitle(String title) {
    this.title = title;
    }

    public String getDescription() {
    return description;
    }

    public void setDescription(String description) {
    this.description = description;
    }

    public void setTags(Collection<EventTagRelationship> tagRelationships) {
    this.tagRelationships = tagRelationships;
    }

    public Iterable<EventTagRelationship> getTagRelationships() {
    return tagRelationships;
    }

    public EventTagRelationship addTag(TagNode tag) {
    final EventTagRelationship tagRel = new EventTagRelationship(tag, this);
    tagRelationships.add(tagRel);
    return tagRel;
    }

    public Set<TagNode> getTags() {
    return tags;
    }

    public void setTags(Set<TagNode> tags) {
    this.tags = tags;
    }

    @Override
    public String toString() {
    return String.format("%s [%s]", title, id);
    }

    @Override
    public boolean equals(Object o) {
    if(this == o) return true;
    if(o == null || getClass() != o.getClass())
    return false;
    EventNode event = (EventNode)o;
    if(nodeId == null)
    return super.equals(o);
    return nodeId.equals(event.nodeId);

    }
    }

    TagNode:


    import java.util.Collection;
    import java.util.Set;

    import org.neo4j.graphdb.Direction;
    import org.springframework.data.neo4j.annotation.GraphId;
    import org.springframework.data.neo4j.annotation.Indexed;
    import org.springframework.data.neo4j.annotation.NodeEnti ty;
    import org.springframework.data.neo4j.annotation.RelatedT o;
    import org.springframework.data.neo4j.support.index.Index Type;

    @NodeEntity
    public class TagNode {
    @GraphId
    Long nodeId;

    @Indexed(unique=true)
    private String id;

    @Indexed(indexType=IndexType.FULLTEXT, indexName = "tags")
    private String name;

    @RelatedTo(type = "EVENT_TAG", direction = Direction.INCOMING)
    Set<EventNode> events;

    public TagNode() {
    super();
    }

    public TagNode(String id, String name) {
    this.id = id;
    this.name = name;
    }

    public String getId() {
    return id;
    }

    public void setId(String id) {
    this.id = id;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public Collection<EventNode> getEvents() {
    return events;
    }

    @Override
    public String toString() {
    return String.format("%s [%s]", name, id);
    }

    @Override
    public boolean equals(Object o) {
    if(this == o) return true;
    if(o == null || getClass() != o.getClass())
    return false;
    TagNode tag = (TagNode)o;
    if(nodeId == null)
    return super.equals(o);
    return nodeId.equals(tag.nodeId);

    }

    }

    EventTagRelationship:

    package com.clearboxmedia.srs.graph;

    import java.util.Collection;
    import java.util.Set;

    import org.neo4j.graphdb.Direction;
    import org.springframework.data.neo4j.annotation.GraphId;
    import org.springframework.data.neo4j.annotation.Indexed;
    import org.springframework.data.neo4j.annotation.NodeEnti ty;
    import org.springframework.data.neo4j.annotation.RelatedT o;
    import org.springframework.data.neo4j.support.index.Index Type;

    @NodeEntity
    public class TagNode {
    @GraphId
    Long nodeId;

    @Indexed(unique=true)
    private String id;

    @Indexed(indexType=IndexType.FULLTEXT, indexName = "tags")
    private String name;

    @RelatedTo(type = "EVENT_TAG", direction = Direction.INCOMING)
    Set<EventNode> events;

    public TagNode() {
    super();
    }

    public TagNode(String id, String name) {
    this.id = id;
    this.name = name;
    }

    public String getId() {
    return id;
    }

    public void setId(String id) {
    this.id = id;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public Collection<EventNode> getEvents() {
    return events;
    }

    @Override
    public String toString() {
    return String.format("%s [%s]", name, id);
    }

    @Override
    public boolean equals(Object o) {
    if(this == o) return true;
    if(o == null || getClass() != o.getClass())
    return false;
    TagNode tag = (TagNode)o;
    if(nodeId == null)
    return super.equals(o);
    return nodeId.equals(tag.nodeId);

    }

    }

    Thanks so much for the assistance!

  5. #5
    Join Date
    Jul 2012
    Posts
    5

    Default

    Oops, actually I changed my test to test the EventTagRelationship, not the tags themselves. Still fails, but wanted to clarify.

  6. #6
    Join Date
    Jul 2012
    Posts
    5

    Default

    For those that come along later I fixed my problem by using @Fetch before my collection and that now forces it to return the actual set of TagNodes (or relationships) that I want.

Tags for this Thread

Posting Permissions

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