Announcement Announcement Module
Collapse
No announcement yet.
Spring Data Neo4j - NullPointerException with @RelatedToVia Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Data Neo4j - NullPointerException with @RelatedToVia

    Hello,

    I am evaluating spring-data-neo4j, and am quite impressed. I seem to be having a very basic problem, so perhaps someone can piont out what I am missing. I am having trouble with basic insertion of two node entities jointed by a relationsihp entity, resulting in a NPE:

    java.lang.NullPointerException
    at org.springframework.data.neo4j.fieldaccess.OneToNR elationshipEntityFieldAccessorFactory$OneToNRelati onshipEntityFieldAccessor.createSetOfTargetNodes(O neToNRelationshipEntityFieldAccessorFactory.java:9 8)
    at org.springframework.data.neo4j.fieldaccess.OneToNR elationshipEntityFieldAccessorFactory$OneToNRelati onshipEntityFieldAccessor.setValue(OneToNRelations hipEntityFieldAccessorFactory.java:71)
    at org.springframework.data.neo4j.fieldaccess.Default EntityState.setValue(DefaultEntityState.java:113)
    at org.springframework.data.neo4j.support.mapping.Sou rceStateTransmitter.setEntityStateValue(SourceStat eTransmitter.java:78)
    at org.springframework.data.neo4j.support.mapping.Sou rceStateTransmitter.access$100(SourceStateTransmit ter.java:39)
    at org.springframework.data.neo4j.support.mapping.Sou rceStateTransmitter$4.doWithAssociation(SourceStat eTransmitter.java:134)
    at org.springframework.data.mapping.model.BasicPersis tentEntity.doWithAssociations(BasicPersistentEntit y.java:185)
    at org.springframework.data.neo4j.support.mapping.Sou rceStateTransmitter.copyPropertiesTo(SourceStateTr ansmitter.java:130)
    at org.springframework.data.neo4j.support.mapping.Neo 4jEntityConverterImpl.write(Neo4jEntityConverterIm pl.java:149)
    at org.springframework.data.neo4j.support.mapping.Neo 4jEntityPersister$CachedConverter.write(Neo4jEntit yPersister.java:176)
    at org.springframework.data.neo4j.support.mapping.Neo 4jEntityPersister.persist(Neo4jEntityPersister.jav a:238)
    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:295)



    ...

    The node entity:

    Code:
    @NodeEntity
    public class Person {
    
        @GraphId
        private Long nodeId;
    
        private String name;
    
        @RelatedToVia(type="FRIEND", direction=Direction.OUTGOING)
        private Set<Friend> friends = new HashSet<Friend>();
    
        public Person() {
        }
    
        public Person(String name) {
            this.name = name;
        }
    
        public final Long getNodeId() {
            return nodeId;
        }
    
        public final String getName() {
            return name;
        }
    
        public void likes(Person p) {
            friends.add(new Friend(this, p));
        }
    
    }
    The relationship entity:

    Code:
    @RelationshipEntity(type = "FRIEND")
    public class Friend {
    
        @GraphId
        private Long nodeId;
    
        @StartNode
        private Person startNode;
    
        @EndNode
        private Person endNode;
    
        public Friend() {
        }
    
        public Friend(Person startNode, Person endNode) {
            this.startNode = startNode;
            this.endNode = endNode;
        }
    
    
        public final Long getNodeId() {
            return nodeId;
        }
    
    
        public final Person getStartNode() {
            return startNode;
        }
    
    
        public final Person getEndNode() {
            return endNode;
        }
    
    }
    And the unit test:

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration({ "/context-transient.xml" })
    @Transactional
    public class PersonTest  {
    
        @Autowired
        Neo4jTemplate template;    
    
        @Test
        public void testEntity() {
            Person bob = new Person("bob");
            Person jim = new Person("jim");
           
            bob.likes(jim);
            template.save(bob);
        }
        
    }
    Thanks for any pointers. I am using spring-data-neo4j version 2.0.1 with neo4j 1.6.1

  • #2
    Well, first of all your RelationshipEntity is just a plain vanilla relationship, so I wouldn't make it a RelationshipEntity. I would just have a standard l=relationship between two Person nodes with the relationship name of Friend. Now if you want to store extra properties in the relationship, that is when you need a RelationshipEntity.

    Do you need to save jim too? Maybe you have to save jim first, then you can save bob.

    The code looks like my code which is working.

    Good Luck

    Mark

    Comment


    • #3
      Thanks for the reply. Yes, I do have properties to the relationship - I just removed them for the sake of brevity. Either way I get the same results.

      Your second suggestion does solve the problem. This seems to go against what I have seen in the documentation. In my real application, I have many related nodes, so saving every one of them individually before the relationship is setup is going to be rather tedious. I was under the impression that the cascade-upon-insert is done for you. I could be wrong here, but maybe someone could confirm.

      Thanks!

      Comment


      • #4
        You might have hit an issue here, cascading save + creating relationships is now there only for @RelatedTo relationships.

        Can you raise an JIRA issue for this?

        Thanks

        Comment


        • #5
          Thanks, will do.

          Comment


          • #6
            Hello,
            jon550, have you raised an JIRA issue about this?
            I just want to be able to follow the issue because I am facing the same kind of problem.
            Thanks.

            Comment


            • #7
              Still facing similar problem, although I'm not sure that it is the same.

              http://stackoverflow.com/questions/1...-error-on-save

              Anyone knows how this evolved?
              Last edited by pasemes; Aug 25th, 2013, 11:23 AM.

              Comment

              Working...
              X