0

是否有可能请建议我如何去做.. 做一个相同的实体关系..

例如。实体(类人)关联到实体(类人)。

代码:

@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"); 请你告诉我哪里出错了?

提前致谢。

4

2 回答 2

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>();
于 2013-08-24T01:42:53.310 回答
1

除了缺少的 Set 初始化之外,另一件事是 ConnectedPersons 类是 @RelationshipEntity。但是在您的类 Person 中,您将它与 @RelatedTo 注释一起使用,就好像它是 @NodeEntity 一样。您应该在 Person 类中使用 @RelatedToVia 批注。

于 2013-08-25T09:04:54.110 回答