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

  • Neo4j ReferenceNode from spring-data

    Hello,
    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")

    HTH,

    Michael

    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?

    Comment


    • #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

      @NodeEntity
      public class Home {

      @GraphId long id;
      @RelatedTo
      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

      Comment


      • #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.

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

        Comment


        • #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);
          }

          Domain:
          @NodeEntity
          public class Things {


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

          public Things() {
          }

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

          public String getDescription() {
          return description;
          }

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

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

          }

          Thing:
          @NodeEntity
          public class Thing {

          @GraphId
          Long nodeId;
          @Indexed
          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)
          @Fetch
          Iterable<Rating> ratings;
          @RelatedToVia(type = "THINGS", direction = INCOMING)
          Things thingsRefNode;

          ...


          I'm trying to test it with:

          @Test
          public void thingsRelatesToTingsAndHome() {

          Things tref=new Things();
          tref.setDescription("Ciao");
          template.save(tref);
          // Things tref2 = thingsRepository.save(tref);
          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");
          thing1.setThingsRefNode(tref);
          thing2.setThingsRefNode(tref);
          template.save(thing1);
          template.save(thing2);

          // 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(Primitive.java:173)
          at org.neo4j.kernel.impl.core.Primitive.getProperty(P rimitive.java:168)
          at org.neo4j.kernel.impl.core.NodeProxy.getProperty(N odeProxy.java:145)
          at org.springframework.data.neo4j.support.typereprese ntation.IndexingNodeTypeRepresentationStrategy.get JavaType(IndexingNodeTypeRepresentationStrategy.ja va:84)
          at org.springframework.data.neo4j.support.typereprese ntation.IndexingNodeTypeRepresentationStrategy.get JavaType(IndexingNodeTypeRepresentationStrategy.ja va:29)
          at org.springframework.data.neo4j.support.mapping.TRS TypeAliasAccessor.readAliasFrom(TRSTypeAliasAccess or.java:39)
          at org.springframework.data.neo4j.support.mapping.TRS TypeAliasAccessor.readAliasFrom(TRSTypeAliasAccess or.java:29)
          at org.springframework.data.convert.DefaultTypeMapper .readType(DefaultTypeMapper.java:72)
          at org.springframework.data.convert.DefaultTypeMapper .getDefaultedTypeToBeUsed(DefaultTypeMapper.java:1 16)
          at org.springframework.data.convert.DefaultTypeMapper .readType(DefaultTypeMapper.java:93)
          at org.springframework.data.neo4j.support.mapping.Neo 4jEntityConverterImpl.read(Neo4jEntityConverterImp l.java:74)
          at org.springframework.data.neo4j.support.mapping.Neo 4jEntityPersister$CachedConverter.read(Neo4jEntity Persister.java:168)
          at org.springframework.data.neo4j.support.mapping.Neo 4jEntityPersister.createEntityFromState(Neo4jEntit yPersister.java:186)
          at org.springframework.data.neo4j.support.mapping.Neo 4jEntityPersister.persist(Neo4jEntityPersister.jav a:239)
          at org.springframework.data.neo4j.support.mapping.Neo 4jEntityPersister.persist(Neo4jEntityPersister.jav a:227)
          at org.springframework.data.neo4j.support.Neo4jTempla te.save(Neo4jTemplate.java:288)
          at it.example.DomainTest.thingsRelatesToTingsAndHome( DomainTest.java:108)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
          at org.junit.runners.model.FrameworkMethod$1.runRefle ctiveCall(FrameworkMethod.java:44)
          at org.junit.internal.runners.model.ReflectiveCallabl e.run(ReflectiveCallable.java:15)
          at org.junit.runners.model.FrameworkMethod.invokeExpl osively(FrameworkMethod.java:41)
          at org.junit.internal.runners.statements.InvokeMethod .evaluate(InvokeMethod.java:20)
          at org.junit.internal.runners.statements.RunBefores.e valuate(RunBefores.java:28)

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

          <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          <spring.version>3.0.6.RELEASE</spring.version>
          <slf4j.version>1.6.1</slf4j.version>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          <neo4j.version>1.5</neo4j.version>
          <spring-data-graph.version>2.0.0.BUILD-SNAPSHOT</spring-data-graph.version>
          <org.slf4j-version>1.6.1</org.slf4j-version>
          <aspectj.version>1.6.12</aspectj.version>
          </properties>


          your help is greatly apprecciated, thank you

          Comment


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

            Cheers

            Michael

            Comment


            • #7
              Thx for your sharing news

              Comment


              • #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

                Comment


                • #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).

                  Cheers

                  Michael

                  Comment


                  • #10
                    Thanks for your time and merry xmas

                    Comment


                    • #11
                      issue documented here: https://jira.springsource.org/browse/DATAGRAPH-142

                      Code:
                      @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());
                          }

                      Comment

                      Working...
                      X