我试图转换此查询
@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 :(得分: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
有关