Neo4j相同实体关系持久性问题 [英] Neo4j Same Entity Relationship persistence issue
问题描述
是否可以建议我如何做..做同一个实体关系..
Is is possible to pls advice me how to go about.. doing a Same Entity Relationship..
例如 实体(类人)与实体(类人)相关.
For ex. Entity(class Person) relatesTo Entity(class Person).
代码:
@NodeEntity
public class Person
{
@GraphId @GeneratedValue
private Long id;
@Indexed(indexType = IndexType.FULLTEXT, indexName = "searchByPersonName")
private String personName;
@Fetch @RelatedTo(type = "CONNECTS_TO", direction = Direction.BOTH)
private Set<ConnectedPersons> connectedPersons;
public ConnectedPersons connectsTo(Person endPerson, String connectionProperty)
{
ConnectedPersons connectedPersons = new ConnectedPersons(this, endPerson, connectionProperty);
this.connectedPersons.add(connectedPersons); //Null Pointer Here(connectedPersons is null)
return connectedPersons;
}
}
代码:
@RelationshipEntity(type = "CONNECTED_TO")
public class ConnectedPersons{
@GraphId private Long id;
@StartNode private Person startPerson;
@EndNode private Person endPerson;
private String connectionProperty;
public ConnectedPersons() { }
public ConnectedPersons(Person startPerson, Person endPerson, String connectionProperty) { this.startPerson = startPerson; this.endPerson = endPerson; this.connectionProperty = connectionProperty;
}
我正在尝试与同一个班级建立关系.即与Person相连的Persons.当我调用Junit测试时:
I am trying to have a Relationship to the same class.. i.e. Persons connected to the Person.. When I invoke a Junit test :
Person one = new Person ("One");
Person two = new Person ("Two");
personService.save(one); //Works also when I use template.save(one)
personService.save(two);
Iterable<Person> persons = personService.findAll();
for (Person person: persons) {
System.out.println("Person Name : "+person.getPersonName());
}
one.connectsTo(two, "Sample Connection");
template.save(one);
当我尝试执行one.connectsTo(two, "Prop");
时,我得到了Null指针
请您告诉我我要去哪里错了吗?
I get Null pointer when I try to do one.connectsTo(two, "Prop");
Please could you tell where am I going wrong?
提前谢谢.
推荐答案
除了缺少Set的初始化外,另一件事是ConnectedPersons类是@RelationshipEntity.但是在类Person中,您将其与@RelatedTo注释一起使用,就好像它是@NodeEntity.您应该在Person类中使用@RelatedToVia注释.
One other thing besides the missing initialization of the Set is that the class ConnectedPersons is a @RelationshipEntity. But in your class Person you are using it with the @RelatedTo annotation as if it were a @NodeEntity. You should use the @RelatedToVia annotation in the Person class instead.
这篇关于Neo4j相同实体关系持久性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!