I am using Spring Data JPA with a RDBMS in my project. I have a requirement where I have to fetch a single record from the Database from a table which has the latest date. For this I need to use a limit and order by function OR using sub queries. However, I wished to know if i wish for not to use NamedQuery is there a way I can achieve this using Spring Data JPA and QueryDSL.
Asked
Active
Viewed 1.9k times
2 Answers
12
Shouldn't QueryDslPredicateExecutor.findAll(Predicate predicate, Pageable pageable)
do the trick for you? You could hand in a new PageRequest(0, limit)
and thus would get back the first limit
results.

Oliver Drotbohm
- 80,157
- 18
- 225
- 211
-
Thanks for the answer. I did actually find it. It returns a list but works just fine. – Abhishek Jun 16 '11 at 17:36
-
@Olver Gierke The only issue is it runs an unnecessary query to get the count. I just use QueryDSL in these cases, but a LimitOne syntax would be cool. Posted the suggestion [here](https://jira.springsource.org/browse/DATAJPA-82?focusedCommentId=78956&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-78956) – Brad Cupit May 17 '12 at 19:22
-
2It does not if you have a return type of `List` instead of `Page`. – Oliver Drotbohm May 18 '12 at 09:11
-
In Case of QueryDslJpaRepository there is only one method and it always runs a count query. Is there another workaround to this? @OliverGierke – Abhishek Dec 31 '13 at 18:46
-
How about the sort order ? `QueryDslPredicateExecutor.findAll(Predicate predicate, Pageable pageable)` does not include sort order. – Xegara Jun 23 '22 at 09:02
1
How about using the MIN and MAX function to attain this.
SELECT MAX(objDate) FROM Object

Talha Ahmed Khan
- 15,043
- 10
- 42
- 49
-
I was hoping not to use any named queries. By using Names query it is very simple but i was wondering if there was a way around this. – Abhishek Jun 15 '11 at 10:46