Spring Data Neo4j - @RelationshipType issues

匿名 (未验证) 提交于 2019-12-03 03:03:02

问题:

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:

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?



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!