0
votes

I am using spring-data-neo4j. I want the results of the query to be mapped to a non-entity POJO.

This is how the repository looks like

public interface CategoryRepository extends GraphRepository<Category> {

    @Query("Match (:Client {email: {clientEmail}})-[:client]->()" + "-[:owns]->()-[:had]->(a:visit)-[:to]->()-[:category]->(b)"
            + " where a.lastPredictionTime > {startTime} and a.lastPredictionTime < {endTime}" + " with Distinct b.name as category, sum(a.timeSpent) as sum order by sum desc"
            + " return collect({category: category, timeSpent: sum})[{start}..{end}]")
    List<CountDetailsByDate> getTopCategoriesByTimeSpent(@Param("clientEmail") String clientEmail, @Param("start") int start, @Param("end") int end,
            @Param("startDate") long startDate, @Param("endDate") long endDate);  
}

The CountDetailsByDate object is neither a node entity nor a relationship entity, I want the result of the query to be mapped to it. Is there any way to do that?

1
Which version of SDN? - Luanne
Look at the documentation, the @QueryResult annotation is what you need. - remigio

1 Answers

0
votes

You should create a CountDetailsByDate class and annotate it with @QueryResult. You can either define it as an inner class in your CategoryRepository repository interface or you can create it somewhere else. The inner-class code will be like this :

public interface CategoryRepository extends GraphRepository<Category> {

@Query("Match (:Client {email: {clientEmail}})-[:client]->()" + "-[:owns]->()-[:had]->(a:visit)-[:to]->()-[:category]->(b)"
        + " where a.lastPredictionTime > {startTime} and a.lastPredictionTime < {endTime}" + " with Distinct b.name as category, sum(a.timeSpent) as sum order by sum desc"
        + " return collect({category: category, timeSpent: sum})[{start}..{end}]")
List<CountDetailsByDate> getTopCategoriesByTimeSpent(@Param("clientEmail") String clientEmail, @Param("start") int start, @Param("end") int end,
        @Param("startDate") long startDate, @Param("endDate") long endDate);  
}

@QueryResult
class CountDetailsByDate {
  // class variables and ...
}

If you choose to create a separate class , make sure you add the package path to component scan of your Neo4j Config class. It will be something like this :

@Configuration
@EnableTransactionManagement
@EnableNeo4jRepositories(basePackages = {"XXX-PATH-TO-Repo-packages"})
@ComponentScan(basePackages = "YYY-PATH-TO-YOUR-CLASS-Package")
public class Neo4jConfig extends Neo4jConfiguration {
 ....
}

Hope this will help.