37

重複の可能性:
JPA を使用して上位 1 つの結果を選択

次のクエリを作成するときに、テーブル「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クエリに何か問題があります。誰でも私を修正できますか?

4

2 に答える 2

66

limitJPAでは認識されません。query.setMaxResults代わりに次のメソッドを使用できます。

sm = em.createQuery("select m from MasterScrip m where m.type = :type 
        order by m.totalTradedVolume")
    .setParameter("type", type)
    .setMaxResults(2).getResultList()
于 2012-05-23T18:09:07.343 に答える
28

Query setFirstResult and setMaxResultメソッドで解決できます

于 2012-05-23T18:10:42.530 に答える