3
votes

I'm having difficulties retrieving relationships when the relationship type is annotated with a @RelationshipType field.

The relationships look correct in Neoclipse, but I'm retrieving no results in my application.

The code that doesn't work is (simplified):

@NodeEntity
public abstract class Entity {

    @RelatedToVia
    private Collection<Relationship> relationships;

    public Relationship relatedTo(Entity entity, String type) {
        Relationship relationship = new Relationship(type, this, entity);
        relationships.add(relationship);

        return relationship;
    }

    ...
}

and:

@RelationshipEntity
public class Relationship {

    @RelationshipType
    private String type;

    ...
 }

The code that does work is:

@RelationshipEntity(type = "something")
public class Relationship {

   ...
}

However, this doesn't suit my use case (I have a bunch of different Relationship types between arbitrary combinations of Entity instances.

The full test code is below. Agency and Item are both subclasses of Entity.

// Create first entity
Agency arnz = agencyRepository.save(new Agency());
arnz.setCode("ARNZ");
agencyRepository.save(arnz);

// Create second entity
Item r123 = itemRepository.save(new Item());
r123.setCode("R123");

// Create parent/child relationship between entities
r123.relatedTo(arnz, EntityRelationshipType.PARENT);
itemRepository.save(r123);

// Retrieve entity from database
Entity entity = itemRepository.findByCode("R123");

// Verify that relationship is present
assertThat(entity.getRelationships().iterator().hasNext(), is(true));

The final line is where the test is failing. Any clues?

M

PS. I'm a rank amateur with Neo4j and just happened to find @RelationshipType, so I may well be doing something laughably wrong. I hope so!

1
Further experimentation shows that itemRepository.getRelationshipBetween(r123, arnz, Relationship.class, EntityRelationshipType.PARENT); returns the relationship as expected. However again, this isn't what my use case demands so isn't of much use!nullPainter
Also, following on from the test code above, Node node = template.getNode(entity.getId()); and an subsequent call to node.getRelationships(); also correctly returns the relationship, just in a less-useful node4j raw Relationship type. So, clearly a Spring Data implementation issue rather than something fundamentally wrong in my graph?nullPainter
And it works if I change the annotation on the collection to @RelatedToVia(direction = Direction.BOTH, type = EntityRelationshipType.PARENT) (i.e., adding a type). Which again isn't very useful as I require a heterogeneous set of relationship types in the same collection. Or is this just a restriction of the framework? I'm using 2.1.0.BUILD-SNAPSHOT.nullPainter
Should be in now, can you try if it works for you?Michael Hunger

1 Answers

2
votes

Sorry to disappoint you, but during the retrieval the code right now doesn't look for the type class but rather for the type from @RelatedToVia or @RelationshipEntity or the field name relationships as relationship-type. But you're making a valid point, can you please raise in issue in JIRA?

Did you look into template.getRelationshipsBetween ?

Why don't you create individual classes for your relationships? What is the use-case for this approach?