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.  [ 3 posts ] 
Author Message
 Post subject: Select first 100 objects
PostPosted: Mon Dec 18, 2006 5:59 pm 
Newbie

Joined: Wed May 24, 2006 9:52 am
Posts: 5
I have a database table (approx 100000 objects)
i need to obtain the first hundred.
Is it possible to do it using HQL. without using native queries like (select top 100 from myTbl)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 19, 2006 5:14 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
You should use a ScrollableResults, as described here : http://blog.hibernate.org/cgi-bin/blosx ... 2004/08/26

Code:
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

ScrollableResults customers = session.getNamedQuery("GetCustomers")
   .scroll(ScrollMode.FORWARD_ONLY);
int count=0;
while ( customers.next() ) {
   Customer customer = (Customer) customers.get(0);
   
   //do what you want here...

   if ( ++count % 20 == 0 ) {
      //flush a batch of updates and release memory:
      session.flush();
      session.clear();
   }
}

tx.commit();
session.close();


I guess that even if you're using NHibernate, you can use a close way for doing this.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 19, 2006 10:07 am 
Newbie

Joined: Wed May 24, 2006 9:52 am
Posts: 5
i found a better solution

NHibernate.IQuery query = DBManager.Session.CreateQuery("from Race as r ");

query.SetFirstResult(offset);
query.SetMaxResults(numberOfRecords);
System.Collections.IList list = query.List();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.