I am trying to limit the query results by using Limit query. With out limit the query is working as expected.
@Query("SELECT a FROM DrmAdpodTimeSlot a where a.startTime > :startTime order by a.startTime desc")
public List<DrmAdpodTimeSlot> findByStartTime(@Param("startTime") Timestamp startTime);
But When I try to limit the records by using limit (no.of records), as follows,
@Query("SELECT a FROM DrmAdpodTimeSlot a where a.startTime > :startTime order by a.startTime desc limit 2")
public List<DrmAdpodTimeSlot> findByStartTime(@Param("startTime") Timestamp startTime);
From the above query I am getting the following error,
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: limit near line 1, column 110 [SELECT a FROM com.dooreme.domain.DrmAd
podTimeSlot a where a.startTime > :startTime order by a.startTime desc limit 2]
How can I use the order by limit query in spring data jpa query?
You can not add pagination support to the
Query
annotation. There is no need for adding sorting and pagination functionality intoHQL/JPQL
when you're using Spring Data JPA. UsePageable
as the second argument instead, like following:Pageable
encasulates the sort and paging functionality, as spring data jpa doc says:So, you can use either:
Or:
Also: