I need to bring from DB only one single result. How can I do that with JPA?
JavaScript
x
Select top 1 * from table
I tried
“select t from table t”
JavaScript
query.setMaxResults(1);
query.getSingleResult();
but didn’t work. Any other ideas?
Advertisement
Answer
Try like this
JavaScript
String sql = "SELECT t FROM table t";
Query query = em.createQuery(sql);
query.setFirstResult(firstPosition);
query.setMaxResults(numberOfRecords);
List result = query.getResultList();
It should work
UPDATE*
You can also try like this
JavaScript
query.setMaxResults(1).getResultList();