在Spring Data Neo4j 4中进行分页和排序

Shr*_*ser 9 java spring neo4j spring-data-neo4j-4

在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);
}
Run Code Online (Sandbox Code Playgroud)

和相应的测试用例:

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

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

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<?>
Run Code Online (Sandbox Code Playgroud)

这是我的设置:

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')
} 
Run Code Online (Sandbox Code Playgroud)

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

@Test
public void itShouldReturnAllTopicsAsAPage() {

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

  assertNotNull(topics);
}
Run Code Online (Sandbox Code Playgroud)

Jay*_*ard 5

现在允许在查询中使用SortPageable接口使用此功能,并且已在DATAGRAPH-653中进行了修复,并在版本中标记为已修复4.2.0.M1(当前在预发行版本中)。

可能进行以下查询:

@Query("MATCH (movie:Movie {title={0}})<-[:ACTS_IN]-(actor) RETURN actor")
List<Actor> getActorsThatActInMovieFromTitle(String movieTitle, Sort sort);
Run Code Online (Sandbox Code Playgroud)

和:

@Query("MATCH (movie:Movie {title={0}})<-[:ACTS_IN]-(actor) RETURN actor")
Page<Actor> getActorsThatActInMovieFromTitle(String movieTitle, PageRequest page);
Run Code Online (Sandbox Code Playgroud)

摘自Spring Data + Neo4j docs中的Cypher示例

查找Spring数据Neo4j预发布里程碑版本:

您可以在项目页面上查看任何发行版依赖项信息。对于4.2.0.M1构建,Gradle的信息(您可以推断Maven)为:

dependencies {
    compile 'org.springframework.data:spring-data-neo4j:4.2.0.M1'
}

repositories {
    maven {
        url 'https://repo.spring.io/libs-milestone'
    }
}
Run Code Online (Sandbox Code Playgroud)

应该使用任何较新的最终版本。


Vin*_*nce 4

目前这是不可能的。

要启用此功能,我们需要做一些事情。首先,在启动时,我们需要检查查询的关联方法签名并将查询标记为需要分页。然后在运行时调用该方法时,我们需要获取可分页实例,提取页面参数并将它们作为 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)
Run Code Online (Sandbox Code Playgroud)