# How to return multiple fields with Spring Data Neo4j RX?

**URL:** <https://community.neo4j.com/t/how-to-return-multiple-fields-with-spring-data-neo4j-rx/21717>\
**Category:** Spring Data Neo4j & Neo4j-OGM\
**Created:** [July 15, 2020, 11:33pm UTC](https://community.neo4j.com/t/how-to-return-multiple-fields-with-spring-data-neo4j-rx/21717 "2020-07-15T23:33:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiaoyfjohn](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jiaoyfjohn/32/9587_2.png) [@jiaoyfjohn](https://community.neo4j.com/u/jiaoyfjohn)\
**Post date:** [July 15, 2020, 11:33pm UTC](https://community.neo4j.com/t/how-to-return-multiple-fields-with-spring-data-neo4j-rx/21717/1 "2020-07-15T23:33:34Z")

</div>

Hi there,

I am using Spring Data Neo4j RX. And I have a query like this:

```auto
@Query("MATCH (a:Repo)-[:REPO_DEPEND_ON]->(b:Repo) WHERE a.name= $name RETURN a.name, b.name")
    String[] getSingleRepoDependencyTo(String name);

```

I know the return type is wrong here, as it cannot be a String array. But how can I get the result properly, which contains two fields?

I have searched online for a long time but cannot find an answer. The "@QueryResult" annotation is not supported in this RX version yet.

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![gerrit.meier](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/gerrit.meier/32/27374_2.png) [@gerrit.meier](https://community.neo4j.com/u/gerrit.meier)\
**Post date:** [July 17, 2020, 7:56am UTC](https://community.neo4j.com/t/how-to-return-multiple-fields-with-spring-data-neo4j-rx/21717/2 "2020-07-17T07:56:20Z")

</div>

Answered: [How to return multiple fields with Spring Data Neo4j RX? - Stack Overflow](https://stackoverflow.com/a/62943632/2650436)

---

<div class="post-metadata">

**Author:** ![Bennu](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/bennu/32/11659_2.png) [@Bennu](https://community.neo4j.com/u/Bennu)\
**Post date:** [November 6, 2020, 12:12am UTC](https://community.neo4j.com/t/how-to-return-multiple-fields-with-spring-data-neo4j-rx/21717/3 "2020-11-06T00:12:39Z")

</div>

Hi @gerrit.meier !

Does this answer work for 'mixed content'?

I've been trying to use this as a reference without positive result.

If I have something like

```auto
@Query("MATCH(n:Person)-[:LIKES]->(m:Movies) return n as person , collect(m) as listOfMovies")

```

How can I define my repository in order to use this query on Rx? In other libraries the @QueryResult has worked.

I have my `Person` and `Movies` entities properly working with the `ReactiveNeo4jRepository` independently tho.

Thanks in advance,

H

---

<div class="post-metadata">

**Author:** ![gerrit.meier](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/gerrit.meier/32/27374_2.png) [@gerrit.meier](https://community.neo4j.com/u/gerrit.meier)\
**Post date:** [November 6, 2020, 7:22am UTC](https://community.neo4j.com/t/how-to-return-multiple-fields-with-spring-data-neo4j-rx/21717/4 "2020-11-06T07:22:00Z")

</div>

You have also to return the relationship.  
I am assuming that the `LIKES` relationship is part of the `Person` entity, right?

A side note: please do not use SDN/RX in the future but SDN 6 (its successor).  
It is available within the Spring Boot RC1 that will get released next week.

---

<div class="post-metadata">

**Author:** ![Bennu](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/bennu/32/11659_2.png) [@Bennu](https://community.neo4j.com/u/Bennu)\
**Post date:** [November 6, 2020, 9:36am UTC](https://community.neo4j.com/t/how-to-return-multiple-fields-with-spring-data-neo4j-rx/21717/5 "2020-11-06T09:36:56Z")

</div>

Hi @gerrit.meier !

Thanks for the quick response.

Actually, the query I've been trying looks more like

```auto
@Query("MATCH(n:Person)-[:LIKES]->(m:Movies) return n as person , count(m) as nMovies")

```

or even

```auto
@Query("MATCH(n:Person) return n as person , size(keys(n)) as nMagic")

```

So the mix is between a `Person` entity and an Integer (last scenario w/o relations). I just tho that giving a much more complex case was going to give me better insight. I have 'worked around' it with apoc.merge and handling it as a single @Node class. Is there another way?

About the side note, can you tell me more about the benefits of the new SDN 6? What's not so good on Rx? We have recently ported an application to the RX library and I want to be sure there's and stable version before moving again. 🙂

Thanks in advance!

H

---

<div class="post-metadata">

**Author:** ![gerrit.meier](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/gerrit.meier/32/27374_2.png) [@gerrit.meier](https://community.neo4j.com/u/gerrit.meier)\
**Post date:** [November 6, 2020, 3:39pm UTC](https://community.neo4j.com/t/how-to-return-multiple-fields-with-spring-data-neo4j-rx/21717/6 "2020-11-06T15:39:30Z")

</div>

SDN 6 is the future of SDN/RX. There is no future work done in the RX project.  
We moved all the sources to VMWare's Spring Data repository and continued the work there ~5 months ago.  
Spring Data Neo4j 6 got release last week as GA and will be included in Spring Boot 2.4 next week.  
What you want to do is exactly what we made possible in SDN 6: Projections.

So for example, you can define a POJO like this:

```auto
public class PersonAndMagic {

    private Person person;
    private long nMagic;
    //...
}

```

---

<div class="post-metadata">

**Author:** ![Bennu](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/bennu/32/11659_2.png) [@Bennu](https://community.neo4j.com/u/Bennu)\
**Post date:** [November 24, 2020, 1:54pm UTC](https://community.neo4j.com/t/how-to-return-multiple-fields-with-spring-data-neo4j-rx/21717/7 "2020-11-24T13:54:39Z")

</div>

Hi @gerrit.meier,

I have tested this projection as you suggested on SDN 6 but I'm not getting the expected result. The extra variable (in this examle nMagic) is properly mapped but the 'original' entity is not mapped.

I modeled an example in [GitHub - BennuFire/nested-relation-query-sdn6](https://github.com/BennuFire/nested-relation-query-sdn6) failing on test test\_projection. I would really appreciate if you can spend a couple of minute checking in case it's my mistake (even if I have tried to follow the SDN 6 documentation).

Thanks!

Harold

---

<div class="post-metadata">

**Author:** ![Bennu](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/bennu/32/11659_2.png) [@Bennu](https://community.neo4j.com/u/Bennu)\
**Post date:** [November 25, 2020, 1:18pm UTC](https://community.neo4j.com/t/how-to-return-multiple-fields-with-spring-data-neo4j-rx/21717/8 "2020-11-25T13:18:10Z")

</div>

Hi everyone,

Thanks to @florent_biville for pointing out the solution.

> If the store optimizes the query execution by limiting the fields to be loaded, the fields to be loaded are determined from the parameter names of the **constructor** that is exposed

The constructor for the projection DTO is mandatory in this cases.

Thanks again,

H
