2013-08-23 79 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。我只是在這裏複雜的東西..但是,它的解決:) –