Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use LIMIT in spring within sql query?

Tags:

Somehow I cannot use the LIMIT qualifier within a sql query using Spring-data-jpa:

@Query("SELECT p from Person p WHERE p.company.id = :id ORDER BY p.name DESC LIMIT 3") 

What is wrong here?

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: Limit near line 1, column 146     at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:91)     at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:109)     at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:304)     at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203)     at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)     at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)     at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)     at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)     at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)     at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)     at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)     at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328)     ... 48 more 
like image 226
membersound Avatar asked Jun 23 '14 13:06

membersound


People also ask

Can we use limit in JPA query?

Conclusion. Limiting query results in JPA is slightly different to SQL; we don't include the limit keyword directly into our JPQL. Instead, we just make a single method call to Query#maxResults, or include the keyword first or top in our Spring Data JPA method name. As always, the code is available over on GitHub.

How do I set the fetch size in JPA?

hibernate. fetchSize", "100"); Or when using Spring Data JPA use a @QueryHints annotation on the interface method. Can be applied to both methods with and without @Query .

What is PagingAndSortingRepository?

PagingAndSortingRepository is an extension of CrudRepository to provide additional methods to retrieve entities using pagination and sorting.

How does JPA pagination work?

The simplest way to implement pagination is to use the Java Query Language – create a query and configure it via setMaxResults and setFirstResult: Query query = entityManager. createQuery("From Foo"); int pageNumber = 1; int pageSize = 10; query. setFirstResult((pageNumber-1) * pageSize); query.


2 Answers

LIMIT is not part of JPQL. The mechanism available in current release version (1.6.0.RELEASE as of the time of writing) is pagination:

interface PersonRepository extends Repository<Person, Long> {    @Query("...")   List<Person> findLimited(..., Pageable pageable); } 

This can then be used as follows:

repository.findLimited(..., new PageRequest(0, 10)); 

This will return the first ten results of the query defined in the @Query annotation.

The current master branch of Spring Data JPA already contains a new feature that would allow you to rewrite above query as follows:

interface PersonRepository extends Repository<Person, Long> {    List<Person> findTop3ByCompanyOrderByName(Company company); } 

As of version 1.7.0.M1 (feature already available in snapshots) the query derivation mechanism will understand Top and First in the subject clause To limit the number of results returned.

Update as new PageRequest deprecated you need to usePageRequest.of(0, 10) instead

like image 78
Oliver Drotbohm Avatar answered Sep 17 '22 14:09

Oliver Drotbohm


My response is perhaps very late, but if I can help anyone else, you can use the nativeQuery like this :

@Query(value="SELECT * from person p WHERE p.company_id = :id ORDER BY p.name DESC LIMIT 3", nativeQuery = true) 
like image 45
Sofiane Avatar answered Sep 16 '22 14:09

Sofiane