2012-05-23 9 views
31

Eventuali duplicati:
Select top 1 result using JPACome scrivere ORDER BY e interrogazione limite JPA

desidero per andare a prendere primi 10 risultati sulla base di 'totalTradedVolume' depositato del mio tavolo 'MasterScrip' quando scrivo la seguente 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(); 

ottengo il seguente exc eption:

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([email protected][]) 

qualcosa non va con la mia query jpa. Qualcuno può correggermi?

+1

leggi la mia asnwer a una domanda simile http://stackoverflow.com/questions/6708085/select-top-1 -result-using-jpa/6708151 # 6708151 – Jorge

+0

Non è proprio un duplicato. La domanda simile recupera un risultato arbitrario, questa domanda riguarda l'ottenimento del risultato con il valore più alto per m.totalTradedVolume. –

risposta

59

limit non è riconosciuto in JPA. È invece possibile utilizzare il metodo query.setMaxResults:

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

perché la parola "limite" non funziona? –

+6

@KorayTugay, so che è troppo tardi per rispondere alla tua domanda, ma per avvantaggiare gli altri, 'limit' è specifico per alcuni databasese (mysql) ma' HQL' è mirato per funzionare con tutti i database supportati da hibernate. –

25

È possibile lavorare fuori con Query setFirstResult and setMaxResult metodi

Problemi correlati