2013-08-23 44 views
0

是可以请教我如何去做..做一个相同的实体关系..Neo4j相同的实体关系持久性问题

例如, 实体(类人)relatedTo实体(类人)。

CODE:

@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; 
    } 
} 

CODE:

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

在此先感谢。

回答

1

一两件事,除了设置的缺失的初始化是类ConnectedPersons是@RelationshipEntity。但是在你的类Person中,你使用了@RelatedTo注解,就像它是@NodeEntity一样。您应该在Person类中使用@RelatedToVia注释。

+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>(); 
+0

谢谢,但我做初始化connectedPersons。我只是在这里复杂的东西..但是,它的解决:) –