I am using spring-data-4.1.1 & Neo4j 2.3.2 with ogm annotations
Below is my entity
@NodeEntity(label = "Component")
public class Component extends BaseEntity {
.........
@Relationship(type = Relation.LINK_TO)
private Set<Link> links = new HashSet<>();
@Relationship(type = Relation.PARENT)
private Set<Component> parents = new HashSet<>();
.........
.........
}
And Link class
@RelationshipEntity(type = Relation.LINK_TO)
public class Link extends BaseEntity {
@Property(name = "isSelfLink")
private boolean isSelfLink;
@StartNode
private Component component;
@EndNode
private Component linkComponent;
}
I've removed getter/setter/hashcode/equals for keeping it clean
Now, here is my code to add two component parent/child and a Link
Component parentcomp = new Component(1, name);
Component childcomp = new Component(2, name);
childcomp.getParents().add(parentcomp);
Link link = new Link();
link.setComponent(parentcomp);
link.setLinkComponent(childcomp);
parentcomp.getLinks().add(link);
componentRepository.save(parentcomp,-1);
Now, as per the logic
- object parentcomp property 'parent' should be empty
- object childcomp property 'parent' should have parentcomp object
And parentcomp property 'links' should have childcomp
(parentcomp)----LINKS_TO---->(childcomp)
(parentcomp)<----PARENT----(childcomp)
Note: My equirement is such that we need two way relationship..
But, below is the result when I load parent or child entity
- object parentcomp property 'parent' has both childcomp,parentcomp instead of empty
- object childcomp property 'parent' has both childcomp,parentcomp instead of only parentcomp
This behavior persist until a Neo4j sessions clears out internally. After some time(or after app restart) the mappings shows up correctly.
I tried cleaning up the session using neo4joperations.clear() still problem persists. But if I query
match (c:Component)-[:PARENT]->(p) where c.componentId = {0} return p
results are correct.
I am not sure how to solve this problem...