0

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.

The graph looks okay: graph

endrec
  • 447
  • 6
  • 17
  • Well, without a concrete example and some context this is not really a question. – Martin Preusse Nov 04 '15 at 15:53
  • What is the return type of the custom query method? And the scope of the session in your application? – Luanne Nov 04 '15 at 15:58
  • @Luanne, I kinda realised what is going on. In my test context the object are saved and load back in the same session, so my test does not even query the graph, the response comes from the MappingContext.nodeEntityRegister. So the real question is, how can I get the direct relationships in a custom query? – endrec Nov 04 '15 at 16:12
  • @MartinPreusse I've completely rewritten the question. – endrec Nov 04 '15 at 16:25

1 Answers1

1

At the moment, custom queries do not support a depth parameter (it's on the roadmap), so you have the following options-

a) Use repository.findOne(userId) (the default is depth 1 so it should load AskedBy). Or customize the depth with repository.findOne(userId,depth). Or use Neo4jTemplate.load(type,id,depth)

b) If you need to query on more than the id, use the loadAll methods on the org.neo4j.ogm.session.Session that accept a set of org.neo4j.ogm.cypher.Filter. Examples available in MusicIntegrationTest

c) Continue with the custom query but after you get the entity ID back, load it via the load* methods providing a custom depth.

Luanne
  • 19,145
  • 1
  • 39
  • 51
  • I've tried loadAll(colection, depth), but is did not help. See my update in the question. – endrec Nov 05 '15 at 07:16
  • One possible problem I see is that Question has an outgoing ASKED_BY relationship to User. But in your relationship entity, the order is reversed- startNode=User, endNode=Question – Luanne Nov 05 '15 at 07:23
  • If you can also set debug on, we might have some more insight into the problem – Luanne Nov 05 '15 at 07:24
  • The direction of the relationship was messed up, thanks for pointing it out. It works now, though it would be better if the depth of a custom query were 1. :) Do you have any target for the depth support on custom queries? – endrec Nov 05 '15 at 09:13