Sorting spring数据jpa可分页排序错误

Sorting spring数据jpa可分页排序错误,sorting,spring-data,spring-data-jpa,Sorting,Spring Data,Spring Data Jpa,我使用spring引导(1.3.5)、spring数据、spring数据jpa、jpa(hibernate/hsqldb) 守则: POM: 控制员: @RequestMapping(value = { "/List", "" }) public String list(Model model, @RequestParam(required = false) String searchString, @SortDefault(sort = "code", direction =

我使用spring引导(1.3.5)、spring数据、spring数据jpa、jpa(hibernate/hsqldb)

守则:

POM:

控制员:

@RequestMapping(value = { "/List", "" })
public String list(Model model, @RequestParam(required = false) String searchString,
        @SortDefault(sort = "code", direction = Direction.ASC) @PageableDefault(page = 0, size = 20) Pageable pageable) {
我试着

也一样,但它不起作用

Page<T> page;
if (!isEmpty(searchString))
   page = service.search(searchString, pageable); // <-- ERROR
else
   page = service.findAll(pageable);  // <-- OK
当我调用
findAll(pageable)
时,它工作,但当我调用
search(str,pageable)
(str=“AT”)时,它不工作

Page<T> page;
if (!isEmpty(searchString))
   page = service.search(searchString, pageable); // <-- ERROR
else
   page = service.findAll(pageable);  // <-- OK
浏览器输出:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed May 25 15:16:24 CEST 2016
There was an unexpected error (type=Internal Server Error, status=500).
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: : near line 1, column 151 [SELECT a FROM prueba.entity.AccountType a WHERE a.code LIKE CONCAT('%', :str, '%') OR UPPER(a.name) LIKE UPPER(CONCAT('%', :str, '%')) order by a.code: ASC asc]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: : near line 1, column 151 [SELECT a FROM prueba.entity.AccountType a WHERE a.code LIKE CONCAT('%', :str, '%') OR UPPER(a.name) LIKE UPPER(CONCAT('%', :str, '%')) order by a.code: ASC asc]
SQL无效!额外“:”和重复的“ASC ASC”。

控制台输出:

Hibernate: 
    select
        count(accounttyp0_.id) as col_0_0_ 
    from
        account_type accounttyp0_ 
    where
        accounttyp0_.code like ('%'||?||'%') 
        or upper(accounttyp0_.name) like upper(('%'||?||'%'))
mo.h.hql.internal.ast.ErrorCounter       line 1:151: unexpected token:     :
mo.h.hql.internal.ast.ErrorCounter       line 1:151: unexpected token:     :

antlr.NoViableAltException: unexpected token: :
    at         org.hibernate.hql.internal.antlr.HqlBaseParser.atom(HqlBaseParser.java:3694)     [hibernate-core-4.3.11.Final.jar:4.3.11.Final]

(more and more)

near line 1, column 151 [SELECT a FROM prueba.entity.AccountType a     WHERE a.code LIKE CONCAT('%', :str, '%') OR UPPER(a.name) LIKE     UPPER(CONCAT('%', :str, '%')) order by a.code: ASC asc]; nested exception     is java.lang.IllegalArgumentException:     org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: :     near line 1, column 151 [SELECT a FROM prueba.entity.AccountType a WHERE     a.code LIKE CONCAT('%', :str, '%') OR UPPER(a.name) LIKE     UPPER(CONCAT('%', :str, '%')) order by a.code: ASC asc]] with root cause

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token:     : near line 1, column 151 [SELECT a FROM     prueba.entity.AccountType a WHERE a.code LIKE CONCAT('%', :str, '%') OR     UPPER(a.name) LIKE UPPER(CONCAT('%', :str, '%')) order by a.code: ASC asc]
    at     org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxExcept    ion.java:91) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]

在查询生成过程中,您似乎遇到了这个异常。对于分页,使用spring data jpa,您必须实现分页和排序存储库接口

您必须编写一个方法,该方法将参数作为Pageable page作为参数,并且可以将newPageRequest(0,size)作为参数传递给Pageable,size是要获取的记录数

不要使用@Query,你可以编写一个简单的方法 可以使用spring-data-jpa中支持的所有关键字。您可以检查 下面是支持的关键字


My repository扩展了JpaRepository,如果使用findAll(可分页),它就可以工作,这不是问题。keyworks的问题是,我看不出如何使用“()”构建复杂的sql,比如:where(a=b或(b=c和d=e)和(f=g或h!=j或I=null))。问题是为什么它可以使用findAll(可分页)而且@Query没有使用相同的pageable(由@PageableDefault设置)构建有效的sql?@MiguelAngel我不明白,如果复杂的sql查询为您提供了更简单的方法,比如findall(),它将代表您生成sql查询,为什么您还要编写复杂的sql查询,因为我需要这种复杂的sql。findAll返回所有行,我需要在应用程序中构建复杂的sql。如何构建只包含关键字的复杂sql?请构造并格式化您的问题,由于缺少代码块、半代码块和格式问题,目前无法阅读。抱歉。现在好多了?
@Override
@Transactional(readOnly = true)
public Page<T> search(String str, Pageable pageable) {
    return repository.search(str, pageable);
}
@Repository
public interface EntityRepository extends JpaRepository<T, Integer> {
(...)
@Query(value = "SELECT a FROM #{#entityName} a WHERE a.code LIKE CONCAT('%', :str, '%') OR UPPER(a.name) LIKE UPPER(CONCAT('%', :str, '%'))")
Page<T> search(@Param("str") String str, Pageable pageable);
@Query(value = "SELECT a FROM #{#entityName} a WHERE a.code LIKE CONCAT('%', :str, '%') OR UPPER(a.name) LIKE UPPER(CONCAT('%', :str, '%'))")
List<T> search(@Param("str") String str);
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed May 25 15:16:24 CEST 2016
There was an unexpected error (type=Internal Server Error, status=500).
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: : near line 1, column 151 [SELECT a FROM prueba.entity.AccountType a WHERE a.code LIKE CONCAT('%', :str, '%') OR UPPER(a.name) LIKE UPPER(CONCAT('%', :str, '%')) order by a.code: ASC asc]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: : near line 1, column 151 [SELECT a FROM prueba.entity.AccountType a WHERE a.code LIKE CONCAT('%', :str, '%') OR UPPER(a.name) LIKE UPPER(CONCAT('%', :str, '%')) order by a.code: ASC asc]
Hibernate: 
    select
        count(accounttyp0_.id) as col_0_0_ 
    from
        account_type accounttyp0_ 
    where
        accounttyp0_.code like ('%'||?||'%') 
        or upper(accounttyp0_.name) like upper(('%'||?||'%'))
mo.h.hql.internal.ast.ErrorCounter       line 1:151: unexpected token:     :
mo.h.hql.internal.ast.ErrorCounter       line 1:151: unexpected token:     :

antlr.NoViableAltException: unexpected token: :
    at         org.hibernate.hql.internal.antlr.HqlBaseParser.atom(HqlBaseParser.java:3694)     [hibernate-core-4.3.11.Final.jar:4.3.11.Final]

(more and more)

near line 1, column 151 [SELECT a FROM prueba.entity.AccountType a     WHERE a.code LIKE CONCAT('%', :str, '%') OR UPPER(a.name) LIKE     UPPER(CONCAT('%', :str, '%')) order by a.code: ASC asc]; nested exception     is java.lang.IllegalArgumentException:     org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: :     near line 1, column 151 [SELECT a FROM prueba.entity.AccountType a WHERE     a.code LIKE CONCAT('%', :str, '%') OR UPPER(a.name) LIKE     UPPER(CONCAT('%', :str, '%')) order by a.code: ASC asc]] with root cause

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token:     : near line 1, column 151 [SELECT a FROM     prueba.entity.AccountType a WHERE a.code LIKE CONCAT('%', :str, '%') OR     UPPER(a.name) LIKE UPPER(CONCAT('%', :str, '%')) order by a.code: ASC asc]
    at     org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxExcept    ion.java:91) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]