These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Get the top n records from a list
PostPosted: Tue Feb 26, 2008 3:21 am 
Newbie

Joined: Thu Jan 03, 2008 7:15 am
Posts: 4
Location: Mumbai
Hi all,
I am a newbie to HQL.

I am querying a database using a SQL View. This view returns me a list of records. However, I only want to get the top 10 records using HQL.

So My SQL would be something like this:
select top 10 * from [MyView]

How can I achieve the same using HQL query?? Is there any "top" clause supported by HQL??

_________________
Thanking You in advance

Kind Regards

Pratik Shah


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 26, 2008 5:08 am 
Newbie

Joined: Tue Feb 19, 2008 6:11 pm
Posts: 3
Use http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Query.html#setMaxResults(int):

Code:
Query q = sess.createQuery("select cat.name, cat from DomesticCat cat order by cat.name");
q.setMaxResults(10);
List cats = q.list();


Carsten


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 26, 2008 5:15 am 
Newbie

Joined: Tue Feb 19, 2008 6:11 pm
Posts: 3
Sorry, wrong language. Use this instead:

Code:
IQuery q = sess.CreateQuery("from DomesticCat cat");
q.SetFirstResult(10);
IList cats = q.List();


Carsten


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 26, 2008 10:16 am 
Newbie

Joined: Tue Feb 19, 2008 11:26 am
Posts: 3
carstenpiepel wrote:
Sorry, wrong language. Use this instead:

Code:
IQuery q = sess.CreateQuery("from DomesticCat cat");
q.SetFirstResult(10);
IList cats = q.List();


Carsten


but use
Code:
SetMaxResult(10);
like in his first response


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.