I have an annotated finder method in my repository:
@Query("MATCH (me:User)<-[ab:ASKED_BY]-(q:Question) WHERE id(me) = {0} RETURN q")
Iterable<Question> findQuestionsByUserId(Long id);
My objects like:
@NodeEntity
public class Question {
private AskedBy askedBy;
@Relationship(type = "TAGGED_WITH")
private Set<Tag> tags = new HashSet<>();
//...
}
@RelationshipEntity(type = "ASKED_BY")
public class AskedBy {
@GraphId private Long id;
@StartNode
private User user;
@EndNode
private Question question;
// other props
}
When I call the repository method, the askedBy
field is null
in the result. How can I populate that field with the relationship?
Update:
I have tried to load the relationship with session loadAll(collection) but it did not help.
final Collection<Question> questions = (Collection<Question>) questionRepository.findQuestionsByUserId(user.getId());
final Question q = questions.iterator().next();
System.out.println("After `findQuestionsByUserId`:");
System.out.println("`q.getTags().size()`: " + q.getTags().size());
System.out.println("`q.getAskedBy()`: " + q.getAskedBy());
neo4jOperations.loadAll(questions, 1);
System.out.println("After `neo4jOperations.loadAll(questions, 1)`:");
System.out.println("`q.getTags().size()`: " + q.getTags().size());
System.out.println("`q.getAskedBy()`: " + q.getAskedBy());
final Collection<AskedBy> askedByCollection = neo4jOperations.loadAll(AskedBy.class);
System.out.println("`askedByCollection.size()`: " + askedByCollection.size());
The above snippet outputs
After findQuestionsByUserId
:
q.getTags().size()
: 0
q.getAskedBy()
: null
After neo4jOperations.loadAll(questions, 1)
:
q.getTags().size()
: 1
q.getAskedBy()
: null
askedByCollection.size()
: 0
So it seems the default depth is 0 for the custom query, and for some unknown reason I can not load the relationship entity.