Spring Data Neo4j - @RelationshipType 问题 [英] Spring Data Neo4j - @RelationshipType issues
问题描述
当关系类型用 @RelationshipType
字段注释时,我很难检索关系.
I'm having difficulties retrieving relationships when the relationship type is annotated with a @RelationshipType
field.
关系在 Neoclipse 中看起来是正确的,但我在我的应用程序中没有检索到任何结果.
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;
}
...
}
和:
@RelationshipEntity
public class Relationship {
@RelationshipType
private String type;
...
}
有效的代码是:
@RelationshipEntity(type = "something")
public class Relationship {
...
}
但是,这不适合我的用例(我在 Entity
实例的任意组合之间有一堆不同的 Relationship
类型.
However, this doesn't suit my use case (I have a bunch of different Relationship
types between arbitrary combinations of Entity
instances.
完整的测试代码如下.Agency
和 Item
都是 Entity
的子类.
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
附注.我是 Neo4j 的业余爱好者,碰巧找到了 @RelationshipType
,所以我很可能在做一些可笑的错误.希望如此!
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!
推荐答案
很抱歉让您失望了,但是在检索过程中,现在的代码不会查找类型类,而是查找来自 @RelatedToVia<的类型/code> 或
@RelationshipEntity
或字段名称 relationships
作为关系类型.但是你提出了一个有效的观点,你能在 JIRA 中提出问题吗?
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?
您是否查看了 template.getRelationshipsBetween
?
你为什么不为你的关系创建单独的类?这种方法的用例是什么?
Why don't you create individual classes for your relationships? What is the use-case for this approach?
这篇关于Spring Data Neo4j - @RelationshipType 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!