在Spring Data Neo4j 4中进行分页和排序 [英] Paging and sorting in Spring Data Neo4j 4

查看:547
本文介绍了在Spring Data Neo4j 4中进行分页和排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否在SDN4中对自定义查询进行分页支持?

is there pagination support for custom queries in SDN4?


  • 如果是,它是如何工作的?

  • 如果不是,是否有工作周期?

我有以下Spring Data Neo4j 4存储库:

I have the following Spring Data Neo4j 4 repository:

@Repository
public interface TopicRepository 
  extends GraphRepository<Topic>,IAuthorityLookup {

  // other methods omitted
  @Query("MATCH (t:Topic)-[:HAS_OFFICER]->(u:User) "
    + "WHERE t.id = {0} "
    + "RETURN  u")
  public Page<User> topicOfficers(Long topicId, Pageable pageable);
}

以及相应的测试用例:

@Test
public void itShouldReturnAllOfficersAsAPage() {
  Pageable pageable = new PageRequest(1,10);
  Page<User> officers = topicRepository.topicOfficers(1L, pageable);
  assertNotNull(officers);
}

当我运行测试时,我遇到以下异常

When I run the test, I run into the following exception

Failed to convert from type java.util.ArrayList<?> to type   org.springframework.data.domain.Page<?> for value '[org.lecture.model.User@1]'; 
nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type java.util.ArrayList<?> to type org.springframework.data.domain.Page<?>

这是我的设置:

dependencies {
//other dependencies omitted
  compile("org.neo4j:neo4j-cypher-dsl:2.0.1")


  compile "org.neo4j.app:neo4j-server:2.2.2"

  compile(group: 'org.springframework.data',
          name: 'spring-data-neo4j',
          version: '4.0.0.BUILD-SNAPSHOT')


  compile(group: 'org.springframework.data',
          name: 'spring-data-neo4j',
          version: '4.0.0.BUILD-SNAPSHOT',
          classifier: 'tests')

  testCompile(group: 'org.neo4j',
          name: 'neo4j-kernel',
          version: '2.2.2',
          classifier: 'tests')

  testCompile(group: 'org.neo4j.app',
              name: 'neo4j-server',
              version: '2.2.2',
              classifier: 'tests')

  testCompile(group: 'org.neo4j',
              name: 'neo4j-io',
              version: '2.2.2',
              classifier: 'tests')
} 

我使用的快照应该能够处理分页,因为以下测试运行得很好:

The Snapshot I use should able to handle pagination, since the following test runs just fine:

@Test
public void itShouldReturnAllTopicsAsAPage() {

  Pageable pageable = new PageRequest(1,10);
  Page<Topic> topics = topicRepository.findAll(pageable);

  assertNotNull(topics);
}


推荐答案

目前这不是'可能。

要启用此功能,我们需要做一些事情。首先在启动时,我们需要检查查询的关联方法签名,并将查询标记为需要分页。然后在运行时调用方法时,我们需要获取可分页实例,提取页面参数并将它们作为SKIP和LIMIT子句应用于关联的Cypher查询。最后,返回时,我们需要将结果包装在Page对象中。所以要做到这一点还有一些工作要做。

To enable this feature we'd need to do a few things. First at startup we would need to inspect the query's associated method signature and mark the query as requiring paging. Then at runtime when the method was invoked we'd need to obtain the pageable instance, extract the page parameters and apply them as SKIP and LIMIT clauses to the associated Cypher query. Finally, on return, we'd need wrap the results in a Page object. So there's a bit of work to be done to enable this.

在此期间,您可以尝试将带参数化值的SKIP和LIMIT子句添加到查询中,并将via中的相应值传递给查询方法。我没试过这个,但它应该有效 - 理论上:

In the meantime you could try adding the SKIP and LIMIT clauses with parameterised values to the query, and pass the appropriate values in via to the query method. I haven't tried this, but it should work - in theory:

  @Query("MATCH (t:Topic)-[:HAS_OFFICER]->(u:User) "
+ "WHERE t.id = {0} "
+ "RETURN  u SKIP {1} LIMIT {2}" )
public List<User> topicOfficers(long topicId, long skip, long limit)

这篇关于在Spring Data Neo4j 4中进行分页和排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆