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

  • Spring Data Neo4j - NullPointerException with @RelatedToVia


    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:

    at elationshipEntityFieldAccessorFactory$OneToNRelati onshipEntityFieldAccessor.createSetOfTargetNodes(O 8)
    at elationshipEntityFieldAccessorFactory$OneToNRelati onshipEntityFieldAccessor.setValue(OneToNRelations
    at EntityState.setValue(
    at rceStateTransmitter.setEntityStateValue(SourceStat
    at rceStateTransmitter.access$100(SourceStateTransmit
    at rceStateTransmitter$4.doWithAssociation(SourceStat
    at tentEntity.doWithAssociations(BasicPersistentEntit
    at rceStateTransmitter.copyPropertiesTo(SourceStateTr
    at 4jEntityConverterImpl.write(Neo4jEntityConverterIm
    at 4jEntityPersister$CachedConverter.write(Neo4jEntit
    at 4jEntityPersister.persist(Neo4jEntityPersister.jav a:238)
    at 4jEntityPersister.persist(Neo4jEntityPersister.jav a:227)


    The node entity:

    public class Person {
        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) {
   = 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:

    @RelationshipEntity(type = "FRIEND")
    public class Friend {
        private Long nodeId;
        private Person startNode;
        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:

    @ContextConfiguration({ "/context-transient.xml" })
    public class PersonTest  {
        Neo4jTemplate template;    
        public void testEntity() {
            Person bob = new Person("bob");
            Person jim = new Person("jim");
    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



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



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



        • #5
          Thanks, will do.


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


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


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