cómo escribir una consulta de orden por y límite en jpa [duplicar]


Posible Duplicado:
Seleccione el resultado top 1 usando JPA

Deseo obtener los 10 mejores resultados basados en 'totalTradedVolume' archivado en mi tabla 'MasterScrip' cuando escribo la siguiente consulta:

Collection<MasterScrip> sm=null;
   sm=em.createQuery("select m from MasterScrip m where m.type = :type order by m.totalTradedVolume limit 2").setParameter("type", type).getResultList();

Obtengo la siguiente excepción:

Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing the query [select m from MasterScrip m where m.type = :type order by m.totalTradedVolume limit 2], line 1, column 78: unexpected token [limit].
Internal Exception: NoViableAltException(80@[])

Algo está mal con mi consulta jpa. ¿alguien puede corregirme?

Author: Community, 2012-05-23

2 answers

limit no se reconoce en JPA. En su lugar, puede usar el método query.setMaxResults:

sm = em.createQuery("select m from MasterScrip m where m.type = :type 
        order by m.totalTradedVolume")
    .setParameter("type", type)
    .setMaxResults(2).getResultList()
 59
Author: Hari Menon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-04 11:31:33

Puede resolverlo con Query setFirstResult and setMaxResult métodos

 25
Author: RP-,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-05-23 18:10:42