如何在jpa中编写顺序并限制查询[重复]

时间:2021-08-29 07:51:33

Possible Duplicate:
Select top 1 result using JPA

可能重复:使用JPA选择前1个结果

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

我希望在写下以下查询时根据我的表'MasterScrip'提交的'totalTradedVolume'获取前10个结果:

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?

我的jpa查询有问题。任何人都可以纠正我吗?

2 个解决方案

#1


59  

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

JPA不承认限制。您可以改为使用query.setMaxResults方法:

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

#2


25  

You can work it out with Query setFirstResult and setMaxResult methods

您可以使用Query setFirstResult和setMaxResult方法来解决它

#1


59  

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

JPA不承认限制。您可以改为使用query.setMaxResults方法:

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

#2


25  

You can work it out with Query setFirstResult and setMaxResult methods

您可以使用Query setFirstResult和setMaxResult方法来解决它