When you create your JPA query object, you can call the following methods on it:
query.setFirstResult(0)
query.setMaxResults(10)
Question
In SQL we have "select top 10" so that only the first 10 rows will be selected. Is there any way in JPA query language to achieve the same effect?
Solution
When you create your JPA query object, you can call the following methods on it:
query.setFirstResult(0)
query.setMaxResults(10)
OTHER TIPS
You can add setMaxResults(10)
to your Query
in code.
For instance:
TypedQuery<MyObject> query = entityManager.createNamedQuery("MyObject.findAll", MyObject.class).setMaxResults(10);