如何在jpa中编写顺序并限制查询 [英] how to write order by and limit query in jpa
本文介绍了如何在jpa中编写顺序并限制查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
使用JPA选择前1名结果
我希望当我写下以下查询时,根据我的表'MasterScrip'
的'totalTradedVolume'获取前10名结果:
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();
我收到以下异常:
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@[])
我的jpa查询有问题。任何人都可以纠正我吗?
something's wrong with my jpa query. can anyone pls correct me?
推荐答案
限制
在JPA中无法识别。您可以改为使用 query.setMaxResults
方法:
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()
这篇关于如何在jpa中编写顺序并限制查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文