您的位置:首页 > 产品设计 > UI/UE

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

2016-03-22 10:43 531 查看
原址:点击打开链接

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?
解决办法:

34down
voteaccepted
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()


 
limit
 is
specific to some databasese (mysql) but 
HQL
 is
targetted to work with all the hibernate supported database. 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: