SDN4中对自定义查询有分页支持吗?

  • 如果是,它如何工作?
  • 如果否,是否有工作环境?

  • 我有以下Spring Data Neo4j 4存储库:
    @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);
    }
    

    运行测试时,遇到以下异常
    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')
    }
    

    我使用的Snapshot应该能够处理分页,因为以下测试运行得很好:
    @Test
    public void itShouldReturnAllTopicsAsAPage() {
    
      Pageable pageable = new PageRequest(1,10);
      Page<Topic> topics = topicRepository.findAll(pageable);
    
      assertNotNull(topics);
    }
    

    最佳答案

    目前,这是不可能的。

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

    同时,您可以尝试将带有参数化值的SKIP和LIMIT子句添加到查询中,并将适当的值通过via传递给查询方法。我还没有尝试过,但是它应该可以工作-理论上:

      @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)
    

    10-02 04:05
    查看更多