意外标记:将 spring data jpa 与 hibernate 一起使用时的位置

Dom*_*ngo 1 hibernate jpa spring-data-jpa

我正在尝试在我的项目中使用 spring data jpa 和 hibernate。我在存储库中添加了注释@Query,尝试编写一个带有Pageable参数的hql,如下所示:

@Query("select name,code,id from Region where fatherId is NULL or fatherId=''") Page<Region> findAllRoots(Pageable pageable);

但是当我尝试编译并运行它时,我得到了意想不到的标记:在控制台上打印。完整信息是:

Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: where near line 1, column 14 [select count(where) from com.nitccs.demo.entity.Region where fatherId is NULL or fatherId='']

怎么就这样跑了?我完全困惑了。为什么不是 select count(id) 之类的?我确信我的 pojo 中没有名为 where 的变量。

Jen*_*der 5

您的查询中需要一个别名,以便Pageable正确创建结果所需的计数查询。所以这应该有效。

select r from Region r where r.fatherId is NULL or r.fatherId=''
Run Code Online (Sandbox Code Playgroud)