Domingo
Domingo

Reputation: 15

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.

Upvotes: 1

Views: 10266

Answers (2)

Musaddique S
Musaddique S

Reputation: 1567

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.

Upvotes: 0

Jens Schauder
Jens Schauder

Reputation: 81988

You need an alias in your query in order for the count query necessary for Pageable results getting created correctly. So this should work.

select r from Region r where r.fatherId is NULL or r.fatherId=''

Upvotes: 5

Related Questions