java - PagingAndSortingRepository 方法在与 spring data jdbc 一起使用时抛出错误

标签 java spring-data spring-data-jdbc

所有与 CrudRepository 相关的 CRUD 方法都按预期工作,但是当尝试使用 findAll 进行分页或排序时会抛出错误。我正在使用 Lovelace-SR2 发布序列。该类只有一个字段,即@Id。根据文档支持分页和排序。 https://docs.spring.io/spring-data/jdbc/docs/1.0.2.RELEASE/reference/html/#repositories

// this throws error
Iterable<FirstAggregate> aggregates = repo.findAll(Sort.unsorted());

public class FirstAggregate {
    @Id
    private UUID id;

    public FirstAggregate(UUID id) {
        this.id = id;
    }

    public FirstAggregate() {
    }

    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }
}

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-releasetrain</artifactId>
    <version>Lovelace-SR2</version>
    <scope>import</scope>
    <type>pom</type>
 </dependency>
 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jdbc</artifactId>
 </dependency>

Caused by: java.lang.IllegalStateException: No query specified on findAll
at org.springframework.data.jdbc.repository.support.JdbcRepositoryQuery.determineQuery(JdbcRepositoryQuery.java:132) ~[spring-data-jdbc-1.0.2.RELEASE.jar:1.0.2.RELEASE]
at org.springframework.data.jdbc.repository.support.JdbcRepositoryQuery.execute(JdbcRepositoryQuery.java:90) ~[spring-data-jdbc-1.0.2.RELEASE.jar:1.0.2.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:605) ~[spring-data-commons-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59) ~[spring-data-commons-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) ~[spring-aop-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61) ~[spring-data-commons-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at com.sun.proxy.$Proxy55.findAll(Unknown Source) ~[na:na]
at com.example.demo.CommandLineRunner.run(CommandLineRunner.java:22) ~[classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
... 5 common frames omitted

最佳答案

更新:对 PagingAndSortingRepository 的支持已登陆 Spring Data JDBC,并将从 2.0.0 M3` 开始提供。

你正在引入 spring-boot-starter-data-jdbc 但你标记了你的问题 spring-data-jpa

因此,如果您要使用 JPA 依赖项,则必须引入 JPA 依赖项。

如果您想使用 Spring Data JDBC,坏消息是:We don't support paging and sorting yet .

文档没有说明这是受支持的,尽管看起来确实如此。 分页和排序仅在描述Spring Data通用概念的通用部分中引用。

另请参阅:https://jira.spring.io/browse/DATACMNS-1086

关于java - PagingAndSortingRepository 方法在与 spring data jdbc 一起使用时抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53088927/

相关文章:

java - 静态方法中的 SharedPreferences - 尽管传递了默认值,但 Lint 警告 "might be null"?

spring-boot - Spring-Data-Gemfire - 无法联系定位器服务。操作超时或定位器不存在

java - 如何使用 CrudRepository 在 @Query 中获取 Set.size()

pagination - Spring Data JDBC 的键集分页

java - 向 Java 类添加 GUI 功能

java - 如何在java中使用http请求发送文件?

Java扫描仪问题

java - Spring-Data-Neo4J 类多态性

java - spring-data-jdbc deleteBy<field> 方法返回异常结果类型