adly adly - 5 months ago 9
SQL Question

How to get last 5 rows?

I'm trying to get the list of only the last five rows from the table. I've already tried with

limit 5
but no luck .

Here is the method

@Transactional
public List<Poll> findAllLimit5() {
return em.createQuery("select a from Poll a ", Poll.class).getResultList();
}


Any help is much appreciated.

Answer

Here is the modified method

@Transactional
public List<Poll> findAllLimit5() {
    return em
            .createQuery("select a from Poll a ORDER BY a.id_poll DESC ",
                    Poll.class).setMaxResults(5).getResultList();

}