JPQL Query Annotation with Limit and Offset

+1 to what user "his" said in the comment:

"the standard way to solve the fundamental problem is to use PagingAndSortingRepository"

Here's an example. I'm throwing in sorting just as an added bonus:

public interface ArtifactRepo extends JpaRepository<Artifact, Long> {
    Page<Artifact> findByComponentKey(String componentKey, Pageable pageable);
}

(You can use @Query above if you like, but JPQL doesn't itself support limits, as "his" noted.)

Then when calling it, use

PageRequest pageRequest =
    new PageRequest(0, 1, Sort.Direction.DESC, "buildNumber");
Page<Artifact> artifactsPage =
    artifactRepo.findByComponentKey(componentKey, pageRequest);

You can also check out this blogpost I wrote:

http://springinpractice.com/blog/categories/chapter-02-data/


limit is not supported by JPQL. Even without it your queries are not valid JPQL queries (but may be valid HQL - and may work if your JPA provider is tolerant).

A (partial) implementation is needed so you can use the Query interface or the criteria api.

Tags:

Java

Jpa