我的 jpaRepository.findAll() returns 中的可分页参数出现 SqlSyntaxErrorException
Pageable parameter in my jpaRepository.findAll() returns an SqlSyntaxErrorException
我正在使用 JpaRepositories,我想将 Specification
、Pageable
和 Sort
参数组合到我的 findAll()
方法中。
不幸的是,当我执行我的代码时出现这个异常(我使用的是 Oracle 数据库):
Caused by: java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended
[...]
Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
这是我的服务方法:
public Page<Demande> findAllByAdherentWithPagination(Long idAdherent, int page, int perPage){
Specification<Demande> spec = DemandeSpecification.hasAdherent(idAdherent);
//page - 1 because pages starts at 0
PageRequest pr = PageRequest.of(page - 1, perPage, sortByCreationDate());
// -- The code stops at the following line --
Page<Demande> demandes = repository.findAll(spec, pr);
return demandes;
}
public Sort sortByCreationDate(){
// "creationDate" is a property of the class Demande
return new Sort(Sort.Direction.DESC, "creationDate");
}
如果不是这样,我应该如何使用 PageRequest
参数?
repository
变量是一个简单的 JpaRepository :
@Repository
public interface DemandeRepository extends ApiRepository<DemandeEntity> {
}
@NoRepositoryBean
public interface ApiRepository<E> extends JpaRepository<E, Long>, JpaSpecificationExecutor<E> {
}
如果您觉得有用,这是我的指定方法:
public static Specification<Demande> hasAdherent(Long idAdherent) {
return (root, query, criteriaBuilder) -> {
return criteriaBuilder.equal(root.get(Demande_.idAdherent), idAdherent);
};
}
当我像这样调用 findAll()
时,在我添加 PageRequest
之前代码曾经工作过:
public List<Demande> findAllByAdherentWithPagination(Long idAdherent, int page, int perPage) throws ApiException {
Specification<Demande> spec = DemandeSpecification.hasAdherent(idAdherent);
List<Demande> demandes = repository.findAll(spec, sortByCreationDate());
// [...] -> Pagination "homemade" using demandes.subList(start, end)
return demandes;
}
原来我从未在 application.properties 中声明我的 Oracle 数据库的版本。
所以 SQL 看起来像这样 :
SELECT [...] FROM [...] WHERE [...] FECTH FIRST 10 ROWS;
但是这个语法只出现在Oracle 12c,我的数据库是Oracle 10g。为了修复它,我将其添加到 application.properties 文件中:
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
我正在使用 JpaRepositories,我想将 Specification
、Pageable
和 Sort
参数组合到我的 findAll()
方法中。
不幸的是,当我执行我的代码时出现这个异常(我使用的是 Oracle 数据库):
Caused by: java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended
[...]
Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
这是我的服务方法:
public Page<Demande> findAllByAdherentWithPagination(Long idAdherent, int page, int perPage){
Specification<Demande> spec = DemandeSpecification.hasAdherent(idAdherent);
//page - 1 because pages starts at 0
PageRequest pr = PageRequest.of(page - 1, perPage, sortByCreationDate());
// -- The code stops at the following line --
Page<Demande> demandes = repository.findAll(spec, pr);
return demandes;
}
public Sort sortByCreationDate(){
// "creationDate" is a property of the class Demande
return new Sort(Sort.Direction.DESC, "creationDate");
}
如果不是这样,我应该如何使用 PageRequest
参数?
repository
变量是一个简单的 JpaRepository :
@Repository
public interface DemandeRepository extends ApiRepository<DemandeEntity> {
}
@NoRepositoryBean
public interface ApiRepository<E> extends JpaRepository<E, Long>, JpaSpecificationExecutor<E> {
}
如果您觉得有用,这是我的指定方法:
public static Specification<Demande> hasAdherent(Long idAdherent) {
return (root, query, criteriaBuilder) -> {
return criteriaBuilder.equal(root.get(Demande_.idAdherent), idAdherent);
};
}
当我像这样调用 findAll()
时,在我添加 PageRequest
之前代码曾经工作过:
public List<Demande> findAllByAdherentWithPagination(Long idAdherent, int page, int perPage) throws ApiException {
Specification<Demande> spec = DemandeSpecification.hasAdherent(idAdherent);
List<Demande> demandes = repository.findAll(spec, sortByCreationDate());
// [...] -> Pagination "homemade" using demandes.subList(start, end)
return demandes;
}
原来我从未在 application.properties 中声明我的 Oracle 数据库的版本。
所以 SQL 看起来像这样 :
SELECT [...] FROM [...] WHERE [...] FECTH FIRST 10 ROWS;
但是这个语法只出现在Oracle 12c,我的数据库是Oracle 10g。为了修复它,我将其添加到 application.properties 文件中:
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect