I need to convert this query to hql to use it my code :
SELECT DISTINCT
certificate_id , last_scan
FROM
bcs_certificate_instance
ORDER BY
last_scan
LIMIT 5 ;
Who can help please?
I need to convert this query to hql to use it my code :
SELECT DISTINCT
certificate_id , last_scan
FROM
bcs_certificate_instance
ORDER BY
last_scan
LIMIT 5 ;
Who can help please?
select distinct bci.certificate_id , bci.last_scan
from bcs_certificate_instance bci
order by last_scan asc;
And for limit you have to use setMaxResults() funtion on query object before executing it.
A code snippet for this would look like:
Query query = em.createQuery(“select distinct bci.certificate_id , bci.last_scan
from bcs_certificate_instance bci
order by last_scan asc;”);
query.setMaxResults(10);
List resultData = query.getResultList();
Thats it. So that is how you limit the number of records returned by a query in HQL.
Example with @Query annotation:
public interface PersonDao extends EntityDao<Person, Long> {
@Query("select p from Person p where p.age between ?1 and ?2")
QueryResult<Person> findAllByAge(int minAge, int maxAge);
}
Once you have obtained a QueryResult, you can apply further options and restrictions to the query:
List<Person> result = personDao.findAllByAge(18, 65)
.sortAsc(Person_.lastName)
.sortDesc(Person_.age).
.firstResult(10)
.maxResults(10)
.getResultList();
}