question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generic abstract RelationshipEntity not working

See original GitHub issue

I started looking at Neo4j today and am now running some tests with OGM. The library is really awesome, thanks for your work! However, when playing around with model abstractions, I encountered the following setup which throws an exception:

@NodeEntity
public class A {
   @GraphId
   private Long id;
   @Relationship(type = "R", direction = Relationship.OUTGOING)
   public R r;
}

@NodeEntity
public class B {
   @GraphId
   private Long id;
   @Relationship(type = "R", direction = Relationship.INCOMING)
   public R r;
}

// Generic base class for relationships
public class BaseR<S, T> {
   @GraphId
   private Long id;
   @StartNode
   private S fromNode;
   @EndNode
   private T toNode;

   protected BaseR(S fromNode, T toNode) {
      this.fromNode = fromNode;
      this.toNode = toNode;
   }
}

@RelationshipEntity
public class R extends BaseR<A, B> {
   public R(A fromNode, B toNode) {
      super(fromNode, toNode);
   }
}

The code I test with is

   @Test
   public void testUsingAbstractRelationShip() {      
      A a = new A();
      session.save(a);

      B b = new B();
      session.save(b);

      R r = new R(a, b);
      a.r = r;
      b.r = r;

      session.save(a); // --> throws exception below
   }

And here comes the exception:

java.lang.RuntimeException: @StartNode of a relationship entity may not be null
    at org.neo4j.ogm.mapper.EntityGraphMapper.getStartEntity(EntityGraphMapper.java:510)
    at org.neo4j.ogm.mapper.EntityGraphMapper.haveRelationEndsChanged(EntityGraphMapper.java:390)
    at org.neo4j.ogm.mapper.EntityGraphMapper.getRelationshipBuilder(EntityGraphMapper.java:363)
    at org.neo4j.ogm.mapper.EntityGraphMapper.link(EntityGraphMapper.java:326)
    at org.neo4j.ogm.mapper.EntityGraphMapper.mapEntityReferences(EntityGraphMapper.java:277)
    at org.neo4j.ogm.mapper.EntityGraphMapper.mapEntity(EntityGraphMapper.java:158)
    at org.neo4j.ogm.mapper.EntityGraphMapper.map(EntityGraphMapper.java:91)
    at org.neo4j.ogm.session.delegates.SaveDelegate.save(SaveDelegate.java:67)
    at org.neo4j.ogm.session.delegates.SaveDelegate.save(SaveDelegate.java:43)
    at org.neo4j.ogm.session.Neo4jSession.save(Neo4jSession.java:386)
    at .a.b.c.GenericTypesTest.testUsingAbstractRelationShip(GenericTypesTest.java:40)

It’s interesting, as the base class without the generic types S and T does work as expected (although this breaks the reusability of the base relationship entity):

public class BaseR {
   @GraphId
   private Long id;
   @StartNode
   private A fromNode;
   @EndNode
   private B toNode;

   protected BaseR(A fromNode, B toNode) {
      this.fromNode = fromNode;
      this.toNode = toNode;
   }
}

@RelationshipEntity
public class R extends BaseR {
   public R(A fromNode, B toNode) {
      super(fromNode, toNode);
   }
}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
frant-hartmcommented, Jul 28, 2017

Fixed in master (for 3.0.0 release).

It still won’t work for

class Owns<T> extends RelationEntity<User,T> 

use cases, but should work for

class Owns extends RelationEntity<User, User>

We use the types in query generation so this can’t be (at the moment) completely generic.

0reactions
mangrishcommented, Dec 8, 2016

This is still broken. Reopening.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Generic and Abstract Relationship Entity · Issue #54 · neo4j ...
We have the following scenario: 2 NodeEntity class (A and B) linked by a RelationshipEntity (L) which is abstract and parametrized.
Read more >
Trouble with extending abstract generic class
I can't figure out why this isn't working. How do you extend an abstract generic superclass and implement it?
Read more >
Spring Data Neo4j
All attributes of a @Node -annotated class will be persisted as properties of Neo4j nodes and relationships. Without further configuration, the ...
Read more >
Reference - OGM Library
When reporting issues or asking for help on StackOverflow or neo4j-users slack ... the parent class is an abstract class and has a...
Read more >
Chapter 5: The extended relationship entity model(Author ...
It avoids unnecessary nulls in the employee attributes when some employees have characteristics that are not shared by other employees.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found