Friday, 23 August 2013

Neo4j Same Entity Relationship persistence issue

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).
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;
}
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);
I get Null pointer when I try to do one.connectsTo(two, "Prop"); Please
could you tell where am I going wrong?
Thanks in advance.

No comments:

Post a Comment