Search code examples
jpql

How to get most repeated value from table in JPQL?


I'm developing a shopping web site application. I need to get the most popular item in the shop. I'm using JPQL. In other words I need to get the most repeated item code from table. I tried with this-

select o.item from OrderDetail o GROUP BY o.item.id ORDER BY COUNT(*) DESC LIMIT 1

But I got the following error.

unexpected token: LIMIT

How to fix this error?

Thanks in Advance!


Solution

  • You can use the setMaxResults() on the query-Object created by the entity manager to limit the number of results

    for example:

    entityManager.createQuery(QUERY).setMaxResults(1);