繁体   English   中英

如何将此JPQL转换为H2上的CriteriaBuilder?

[英]How do I convert this JPQL to CriteriaBuilder on H2?

我正在尝试转换此查询

@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无效。 如何更正我的标准以便生成所需的结果?

这确实是不相同的查询,但我相信它会达到相同的效果。

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

我有点怀疑规范是否继续适用该命令这一事实是否存在错误……最终这似乎与该错误有关

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM