I have created a node class called MyNode
and an edge class called MyEdge
as follows:
@Node
public class MyNode {
@Id
String id;
String name;
String type;
@Relationship(type = "CONTAINS")
Set<MyEdge> outgoingEdges = new HashSet<>();
public MyNode() {
}
public MyNode(String id, String name, String type) {
this.id = id;
this.name = name;
this.type = type;
}
// Getters and setters...
and
@RelationshipProperties
public class MyEdge {
@Id
String id;
String idNodeFrom;
String idNodeTo;
@TargetNode
MyNode targetNode;
public MyEdge(String idNodeFrom, String idNodeTo, MyNode targetNode) {
this.id = idNodeFrom + " -> " + idNodeTo;
this.idNodeFrom = idNodeFrom;
this.idNodeTo = idNodeTo;
this.targetNode = targetNode;
}
// Getters and setters
}
Note that on MyEdge
I have annotated the field id with @Id
instead of @RelationshipId
as being indicated here. And the reason for this is because somehow IntelliJ can't find the @RelationshipId
annotation although I have the following in my pom.xml
file
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
</dependency>
So my first question is how can I fix that? And my second and most important one is:
Why does the findAll
method of the MyEdge
repository (as seen below) always return an empty list?
public interface MyEdgeNeo4jRepository extends Neo4jRepository<MyEdge, String> {
@Query("MATCH ()-[e]->() RETURN e")
Collection<MyEdge> customFindAllEdges();
}
I have connected to the database through my browser and have verified that the nodes and the relationships are modeled as they should be. But still, when I call
Collection<MyEdge> ret = myEdgeNeo4jRepository.findAll();
for (MyEdge myEdge: ret) {
System.out.println(myEdge);
}
nothing gets printed.
Another thing I have tried is to use a class-based projection, namely the following class
public class MyEdge2 {
String idNodeTo;
String idNodeFrom;
public MyEdge2() {
}
public MyEdge2(String idNodeTo, String idNodeFrom) {
this.idNodeTo = idNodeTo;
this.idNodeFrom = idNodeFrom;
}
// Getters and setters
}
and then try
@Query("MATCH ()-[e]->() RETURN e")
Collection<MyEdge2> customFindAllEdges();
but this approach throws the following exception
org.springframework.data.neo4j.core.mapping.NoRootNodeMappingException: Could not find mappable nodes or relationships inside Record<{e: relationship<5630>}> for org.springframework.data.neo4j.core.mapping.DefaultNeo4jPersistentEntity@504aa259
Any help would be appreciated! Thanks very much in advance!