Reference node - Spring data neo4j

86 views
Skip to first unread message

Abdul Azeez Shaik

unread,
May 16, 2012, 4:03:40 AM5/16/12
to ne...@googlegroups.com
Dear all,

Would reference node get created automatically with id=0 while using SDN? or is it expected behavior even while using normal neo4j APIs?
Can you please confirm me?

I would like to create new relationship between few of my nodes and reference node. Can i do this only for few nodes? or by default relationship is created for all nodes with reference nodes?

Can you please clarify this? Really confused.

Thanks,
 Abdul 

Michael Hunger

unread,
May 16, 2012, 7:14:54 AM5/16/12
to ne...@googlegroups.com
The reference node is created automatically by neo4, not related to SDN at all.
The only time SDN uses the reference node (automatically) is when you use a type representation strategy that stores they type hierarchy as tree of nodes bound to the reference node.

There is a sample test for connecting nodes to a reference node in SDN, you use a Type to represent the root node and then connect to this entity-type.

in spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/GraphRepositoryTest.java

@Test @Transactional
public void testConnectToRootEntity() {
final Node referenceNode = neo4jTemplate.getReferenceNode();
neo4jTemplate.postEntityCreation(referenceNode,RootEntity.class);
final RootEntity root = neo4jTemplate.findOne(referenceNode.getId(), RootEntity.class);
root.setRootName("RootName");
neo4jTemplate.save(root);
assertEquals(referenceNode.getId(), (long) root.getId());
assertEquals("RootName", referenceNode.getProperty("rootName"));
assertEquals("RootName", root.getRootName());

final Person person = new Person();
person.setRoot(root);
neo4jTemplate.save(person);
final Person p2 = neo4jTemplate.findOne(person.getId(), Person.class);
assertEquals(root.getId(),p2.getRoot().getId());
}


@NodeEntity
public class RootEntity {
@GraphId Long id;

String rootName;

public Long getId() {
return id;
}

public String getRootName() {
return rootName;
}

public void setRootName(String rootName) {
this.rootName = rootName;
}
}
Cheers

Michael

Abdul Azeez Shaik

unread,
May 16, 2012, 10:51:24 AM5/16/12
to ne...@googlegroups.com
Thanks for the reply Michael. Below code suits my usecase. I want to add relation between one of the node and root node.
However, postEntityCreation seems to be deprecated in latest version. Is there any other method which does the same job.
or shall i directly use CreateRelationShipBetween for this? 

Thanks,
 Abdul
Reply all
Reply to author
Forward
0 new messages