Generic abstract RelationshipEntity not working
See original GitHub issueI 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:
- Created 8 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Fixed in master (for 3.0.0 release).
It still won’t work for
use cases, but should work for
We use the types in query generation so this can’t be (at the moment) completely generic.
This is still broken. Reopening.