Announcement Announcement Module
No announcement yet.
Neo4j ReferenceNode from spring-data Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Neo4j ReferenceNode from spring-data

    does anybody know how to get reference node from annotated Neo4j Relations?
    I would like to implement guidelines for NodeId declaration and Categorization
    (RefNode-->Users-->User) where Users is the node that tell me what kind of User i have and store the nodeId's sequence to give to everyUser (Users.getNextId()).

    I annotated the model (Users) with @NodeEntity and @GraphId, i declared the outgoing relation (Users-->User) with @RelateTo
    but then i don't know how to get the RefNode to build the incoming relation (Home-->Users).

    does anybody have an example on that or a documentation?
    I' m using Repository Pattern, should i use template?

    thank you for your great work.

  • #2
    Probably create a class that represents the ReferenceNode (e.g. Home.class).

    Then you can load the Home object using template.projectTo(template.getReferenceNode(),Hom e.class).

    Then you can use all the facilities, like setting the Home instance on the Users instance (users.home = home and save the users object..

    Or use use template.createRelationshipBetween(home, users, "USERS", Relationship.class)

    If you just want to create the relationship it is also enough to use this:
    Either you just use template.createRelationshipBetween(template.getRef erenceNode(), template.getPersistentState(users), "USERS")



    P.S: Do you think it would be sensible to collect such things in a central place as FAQ? Where would you look for it first?


    • #3
      thank you for your answer but either wais i get an error:

      with .projectTo method and
      template.createRelationshipBetween(template.getRef erenceNode(), template.getPersistentState(things), Relationship.class, "THINGS", false) call i got

      NotFoundException: __type__ property not found for NodeImpl#0

      given a Home class defined like this

      public class Home {

      @GraphId long id;
      Things things;

      Am i missing something?

      off course i would appreciate a faq sticky thread, but most of all i thing would be of some help an example with only neo4j techniques and model consideration (no hibernate, no hsql) like schema from shcema gallery example

      thank you


      • #4
        Could you please provide the full stack trace?

        This is b/c the reference node has not been created by SDN.

        So there are two ways to solve this.

        First is
        to call template.postEntityCreation(home); which adds the __type__ property and the node to the type index.

        #1 create a new home object and call;
        #2 then graphDb.setReferenceNode(template.getPersistentSta te(home));
        #3 optionally delete the existing reference node and


        • #5
          Sorry, I can't get the point to the solution,
          this is the model i'm trying to express: ReferenceNode->Things->Thing
          I think I'm missing something, here is the code:

          The repository:
          public interface ThingsRepository extends GraphRepository<Things>, NamedIndexRepository<Things>, RelationshipOperationsRepository<Things> {

          Things findById(long nodeId);

          public class Things {

          long id;
          long nodeId;
          String description="No description";

          public Things() {

          public Things(long nodeId, String description) {
          this.nodeId = nodeId;
          this.description = description;

          public String getDescription() {
          return description;

          public void setDescription(String description) {
          this.description = description;

          Set<Category> categories;
          Set<Thing> things;


          public class Thing {

          Long nodeId;
          String id;
          @Indexed(indexType = IndexType.FULLTEXT, indexName = "search")
          String name;

          String description;
          @RelatedTo(type = "OWNS", direction = INCOMING)
          User owner;
          @RelatedTo(type = "OWNED", direction = INCOMING)
          Set<User> owners;
          @RelatedToVia(type = "RATED", direction = INCOMING)
          Iterable<Rating> ratings;
          @RelatedToVia(type = "THINGS", direction = INCOMING)
          Things thingsRefNode;


          I'm trying to test it with:

          public void thingsRelatesToTingsAndHome() {

          Things tref=new Things();
          // Things tref2 =;
          Node ths = template.getPersistentState(tref);
          Relationship refRel = template.getReferenceNode().createRelationshipTo(t hs, Relations.THINGS);

          System.out.println("relat " + refRel.getStartNode().getId() + " " + refRel.getEndNode().getId());
          Thing thing1 = new Thing("1", "Thing 1");
          Thing thing2 = new Thing("2", "Thing 2");

          // tref.things.add(thing1);


          and i got the error:

          org.neo4j.graphdb.NotFoundException: __type__ property not found for NodeImpl#0.
          at org.neo4j.kernel.impl.core.Primitive.newPropertyNo tFoundException(
          at org.neo4j.kernel.impl.core.Primitive.getProperty(P
          at org.neo4j.kernel.impl.core.NodeProxy.getProperty(N
          at ntation.IndexingNodeTypeRepresentationStrategy.get JavaType(IndexingNodeTypeRepresentationStrategy.ja va:84)
          at ntation.IndexingNodeTypeRepresentationStrategy.get JavaType(IndexingNodeTypeRepresentationStrategy.ja va:29)
          at TypeAliasAccessor.readAliasFrom(TRSTypeAliasAccess
          at TypeAliasAccessor.readAliasFrom(TRSTypeAliasAccess
          at .readType(
          at .getDefaultedTypeToBeUsed( 16)
          at .readType(
          at 4jEntityPersister$
          at 4jEntityPersister.createEntityFromState(Neo4jEntit
          at 4jEntityPersister.persist(Neo4jEntityPersister.jav a:239)
          at 4jEntityPersister.persist(Neo4jEntityPersister.jav a:227)
          at it.example.DomainTest.thingsRelatesToTingsAndHome(
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(Native
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(De
          at org.junit.runners.model.FrameworkMethod$1.runRefle ctiveCall(
          at org.junit.internal.runners.model.ReflectiveCallabl
          at org.junit.runners.model.FrameworkMethod.invokeExpl osively(
          at org.junit.internal.runners.statements.InvokeMethod .evaluate(
          at org.junit.internal.runners.statements.RunBefores.e valuate(

          I can't see the methods you suggest: template.setReferenceNode template.postEntityCreation from template nor the delete one; maybe i' m using wrong version?


          your help is greatly apprecciated, thank you


          • #6
            I'll look at it tomorrow and provide a solution.




            • #7
              Thx for your sharing news


              • #8
                It seems to me also, none of the example code is connecting to RefNode, and none of the example build a database i can browse with NeoClipse or NeoGui.

                Is that a limit of the spring-data porting?

                thank you


                • #9
                  If you use the SubReferenceNodeTypeRepresentationStrategy they'll be connected to the root node via their type hierarchy.

                  But Neoclipse will soon have capabilitiies displaying disconnected nodes (btw. you already can search the index for nodes and display them in Neoclipse).




                  • #10
                    Thanks for your time and merry xmas


                    • #11
                      issue documented here:

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