I have entity, which looks like this:
@NodeEntity
class User {
String name;
boolean male;
<...>
@Relationship(type = "friend")
Set<User> friends;
@Relationship(type = "participation")
Set<Event> events;
<...>
}
In order to query male users I use the following code:
String q = "MATCH (u:User) WHERE u.male=true RETURN u";
Iterable<User> maleUsers = neo4jOperations.queryForObjects(User.class, q, emptyMap());
But it returns users with empty collections of friends and events.
Trying to fix the problem I got the following code:
String q = "MATCH (u:User) WHERE u.male=true WITH u MATCH p=(u)-[*0..1]-() RETURN p";
Iterable<User> maleUsers = neo4jOperations.queryForObjects(User.class, q, emptyMap());
And now collections are initialized. But this change brings a new problem:
How to solve my problem properly? Any thoughts are very appreciated!
PS: I use Spring Data Neo4j 4.1.0.M1.
This is because you return a path which contains many User entities, and the OGM is unable to tell which one you want back.
The best way to do this would be to use session.query
:
Result result = session.query("MATCH (u:User) WHERE u.male=true WITH u MATCH p=(u)-[*0..1]-() RETURN u as user, nodes(p),rels(p)",emptyMap());
and then from result
, retrieve each user
which will have friends and events mapped.