2016-05-25 29 views
0

私はスプリングブート(1.3.5)、スプリングデータ、スプリングデータjpa、JPA(hibernate/hsqldb)を使用します。スプリングデータjpaページングソートエラー

コード:

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

Iは

を試します
@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 whithてみたがPageable pageableパラメータなしintead List<>Page<>を返し、それこれと一緒に使える@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); 

私はそれが動作しますが、私は(STR = "AT")search(str, pageable)を呼び出すとき、それは動作しませんfindAll(pageable)呼び出します。

ブラウザ出力:

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] 
+0

現在、それが原因不足しているコードブロック、半分のコードブロックと書式設定の問題に読めない、あなたの質問を構造化してフォーマットしてください。 –

+0

申し訳ありません。今はまし? –

答えて

1

クエリ生成時にuがexception.Forページネーションは、あなたがPagingAndSortingRepositoryインタフェースを実装する必要があり、スプリング・データ-JPAを使用することを得たようです。ページング可能とサイズの引数として、あなたはとしてページング可能ページなどのパラメータを取り、あなたが新しいPageRequest(0、サイズを)渡すことができる方法を記述する必要が

はあなたが取得したいレコードのnoです。

ではなく、あなたが簡単にメソッドを書くことができ@queryを使用しないでください、あなたは サポートされるキーワードについては、以下の を確認することができますspring-data-jpa.youでサポートされているすべてのキーワードを使用することができます。

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

+0

私のリポジトリはJpaRepositoryを拡張しており、findAll(pageable)を使用すると動作しますが問題ありません。 –

+0

keyworksの問題は、このように '()'という複雑なsqlをどのように構築するのかわかりません:where a = b or(b = c and d = e)and(f = g or h!= jまたはi = null))。 –

+0

質問はなぜfindAll(ページ可能)で動作し、@Queryは同じページング可能な(@ PageableDefaultで設定された)有効なSQLを構築しないのですか? –

関連する問題