2015-10-17 35 views
0

我想這個查詢如何將此JPQL轉換爲H2上的CriteriaBuilder?

@Query("select l from Log l order by l.created desc, l.entry asc ") 
Page<Log> findAllCustomJpql(Pageable pageable); 

生成這個SQL

Hibernate: select count(log0_.id) as col_0_0_ from log log0_ 
Hibernate: select log0_.id as id1_0_, log0_.created as created2_0_, log0_.entry as entry3_0_ from log log0_ order by log0_.created desc, log0_.entry asc limit ? 

的標準建造的查詢,使用規範

@RequestMapping(method = RequestMethod.GET, path = "spec") 
Page<Log> getLogsBySpecification(final Pageable pageable) { 
    return repository.findAll((root, query, cb) -> { 
     query.orderBy(
       cb.desc(root.get("created")), 
       cb.asc(root.get("entry")) 
     ); 
     return null; 
    }, pageable); 
} 

這是做以下

轉換
2015-10-17 19:33:40.720 WARN 10498 --- [nio-8080-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 90016, SQLState: 90016 
2015-10-17 19:33:40.721 ERROR 10498 --- [nio-8080-exec-6] 
o.h.engine.jdbc.spi.SqlExceptionHelper : Column "LOG0_.ENTRY" must be in the GROUP BY list; SQL statement: 
select count(log0_.id) as col_0_0_ from log log0_ order by log0_.created desc, log0_.entry asc [90016-188] 
2015-10-17 19:33:40.750 ERROR 10498 --- [nio-8080-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not extract ResultSet; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet] with root cause 
org.h2.jdbc.JdbcSQLException: Column "LOG0_.ENTRY" must be in the GROUP BY list; SQL statement: 
select count(log0_.id) as col_0_0_ from log log0_ order by log0_.created desc, log0_.entry asc [90016-188] 

我個人認爲sql是有效的,如果生病了,但似乎不適用於h2。我如何糾正我的標準以便生成我想要的結果?

回答

0

這實際上不是同一個查詢,但我確實相信它具有相同的效果。

@RequestMapping(method = RequestMethod.GET, path = "spec") 
Page<Log> getLogsBySpecification(final Pageable pageable) { 
    return repository.findAll((root, query, cb) -> { 
     query.orderBy(
       cb.desc(root.get("created")), 
       cb.asc(root.get("entry")) 
     ); 

     query.groupBy(root.get("id")); 
     return null; 
    }, pageable); 
} 

我有點想知道如果該規範繼續以申請順序其實是一個錯誤......最終這似乎與this bug