gpt4 book ai didi

java - 如何在jpa中编写order by和limit查询

转载 作者:IT老高 更新时间:2023-10-28 20:49:06 26 4
gpt4 key购买 nike

Possible Duplicate:
Select top 1 result using JPA

我希望根据我的表“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();

我得到以下异常:

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 查询有问题。谁能纠正我?

最佳答案

limit 在 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()

关于java - 如何在jpa中编写order by和limit查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10725469/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com