spring data jpa可分页排序错误

时间:2016-05-25 13:57:13

标签: sorting spring-data spring-data-jpa

我使用spring boot(1.3.5),spring-data,spring-data-jpa,JPA(hibernate / hsqldb)。

代码:

POM:

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>

<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>

配置代码:

@Bean
public PageableHandlerMethodArgumentResolver pageableHandlerMethodArgumentResolver(
        SortHandlerMethodArgumentResolver sortHandlerMethodArgumentResolver) {

    PageableHandlerMethodArgumentResolver phmar = new PageableHandlerMethodArgumentResolver(
            sortHandlerMethodArgumentResolver);
    phmar.setOneIndexedParameters(false);
    phmar.setPageParameterName("page");
    phmar.setSizeParameterName("size");
    phmar.setMaxPageSize(20);
    return phmar;
}

@Bean
public SortHandlerMethodArgumentResolver sortHandlerMethodArgumentResolver() {

    SortHandlerMethodArgumentResolver shmar = new SortHandlerMethodArgumentResolver();
    shmar.setSortParameter("sort");
    return shmar;
}

控制器:

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

我试试

@PageableDefault(page = 0, size = 20, sort = "code", direction = Direction.ASC)

,但它不起作用。

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

服务(简化):

@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,但返回List<> intead Page<>而没有Pageable pageable参数,它适用于此@Query

试验:

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

当我致电findAll(pageable)时,它可以正常工作,但是当我拨打search(str, pageable)(str =&#34; AT&#34;)时,它无效。

浏览器输出:

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无效!额外&#34;:&#34;并复制&#34; ASC asc&#34;。

控制台输出:

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]

1 个答案:

答案 0 :(得分:2)

似乎在查询生成期间你得到了这个例外。对于分页使用spring-data-jpa你必须实现 PagingAndSortingRepository 接口。

你必须编写一个方法,将参数作为可分页页面,你可以将新的 PageRequest(0,size)作为参数传递给Pageable,大小是否为你想要获取的记录。

  

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

http://docs.spring.io/spring-data/jpa/docs/1.4.3.RELEASE/reference/html/jpa.repositories.html