Announcement Announcement Module
No announcement yet.
Neo4j custom query in repository with multiple nodes returned Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Neo4j custom query in repository with multiple nodes returned

    Hi, I'm working with neo4j and spring-data and trying to achieve something
    not sure how as it is not mentioned in Good Relationships book.

    I have declared method on repository interface which is annotated with Query:

    @Query(value = "START ev=node:Event(\"*:*\") " +
                "MATCH ev-[r:HAS_RELATED_DATA]->evPop " +
                "WHERE evPop.popularityId = {0} " +
                "RETURN ev, evPop " + <-- notice multiple return nodes
                "ORDER BY evPop.betCount DESC")
    public List<Event> findByGivenPopularityId(String partnerId);
    Has related data relationship is one to many and I do have Set<PopularityData> field within Event
    annotated with @RelatedTo.

    However it is not working complaining:
    Cannot extract single value from Iterable with more than one elements
    Removing second node from returning set makes this work as expected.
    I suspect that put like this framework does not know what to do with second returned value as
    it can't guess if it is related or not to first one, so question is:

    Is it possible at all to achieve something like this where query returns multiple entities.
    I am aware of ways to work around but just curious if there is how to say... "nice and clean, spring-data way"
    to do this?

    Any tip/suggestion/advice is welcome and appreciated.

    Thanks in advance,

  • #2
    try to return the object like example below:
    public Iterable<Map<String, Object>> findByGivenPopularityId(String partnerId);
    u can try to read the list like below:
    Iterable<Map<String, Object>> results = yourRepository.findByGivenPopularityId("yourPartnerId"); for (Map<String, Object> r: results) { for (Map.Entry<String, Object> entry : r.entrySet()) { System.out.println(entry.getKey()+"->"+entry.getValue()); } }


    • #3
      Please, move your question to the StackOverflow - we are going to close this forum soon and rely on SO.
      We need to clean this forum (old unanswered question) before close it.

      Thanks for understanding