how to write order by and limit query in jpa [duplicate]

这一生的挚爱 提交于 2019-11-30 06:33:59

问题


Possible Duplicate:
Select top 1 result using JPA

i wish to fetch top 10 results based on 'totalTradedVolume' filed of my table 'MasterScrip' when i write the following query:

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();

i get the following exception :

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@[])

something's wrong with my jpa query. can anyone pls correct me?


回答1:


limit is not recognized in JPA. You can instead use the query.setMaxResults method:

sm = em.createQuery("select m from MasterScrip m where m.type = :type 
        order by m.totalTradedVolume")
    .setParameter("type", type)
    .setMaxResults(2).getResultList()



回答2:


You can work it out with Query setFirstResult and setMaxResult methods



来源:https://stackoverflow.com/questions/10725469/how-to-write-order-by-and-limit-query-in-jpa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!