hibernatejpaspring-data-jpa

unexpected token:where when using spring data jpa with hibernate


I am trying to use spring data jpa and hibernate in my project. I added the annotation @Query in repository, trying to write a hql with a Pageable argument passed in like this:

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

but when I tried to compile and run it, I got unexpected token: where printed on console. Full info is:

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='']

How could it ran like this? I am totally confused. Why it is not select count(id) or something? I am sure I got no variable named where in my pojo.


Solution

  • Your above query is incorrect. You are expecting name,code,id to convert into Region object.

    If you want data with pagination, try to use SpringData specifications

    See the Advanced Spring Data JPA - Specifications and Querydsl for more information.