是否可以建议我如何去做...做同样的实体关系..
对于前。 实体(类Person)涉及实体(类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;
}
我正在尝试与同一个班级建立关系..即连接到此人的人..当我调用Junit测试时:
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指针
请问你能告诉我哪里出错了吗?
提前致谢。
答案 0 :(得分:1)
您在以下代码中获得空指针异常,因为您尚未初始化connectedPersons
集合。
this.connectedPersons.add(connectedPersons); //Null Pointer Here(connectedPersons is null)
初始化集合,如下所示
@Fetch @RelatedTo(type = "CONNECTS_TO", direction = Direction.BOTH)
private Set<ConnectedPersons> connectedPersons=new HashSet<ConnectedPersons>();
答案 1 :(得分:1)
除了缺少初始化Set之外的另一件事是ConnectedPersons类是@RelationshipEntity。但是在你的类Person中,你正在使用@RelatedTo注释,就像它是@NodeEntity一样。您应该在Person类中使用@RelatedToVia批注。