-->
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: What is a right/better way to retrieve the first item?
PostPosted: Mon Jun 29, 2009 2:46 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 3:36 pm
Posts: 78
I need to retrieve the first entry from a HQL. I can thinking of two ways to do it.
Code:
getSession().createQuery("from Message m order by m.date desc").list().get(0);

and
Code:
getSession().createQuery("from Message m order by m.date desc").setMaxResults(1);

Which one is right or better?


Top
 Profile  
 
 Post subject: Re: What is a right/better way to retrieve the first item?
PostPosted: Mon Jun 29, 2009 2:57 pm 
Newbie

Joined: Mon Jun 29, 2009 1:45 pm
Posts: 3
Code:
getSession().createQuery("from Message m order by m.date desc").setMaxResults(1);


because hibernate add's a restriction to the query like "LIMIT 1" ... the other solution prepares a result set with all message's


Top
 Profile  
 
 Post subject: Re: What is a right/better way to retrieve the first item?
PostPosted: Mon Jun 29, 2009 3:42 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 3:36 pm
Posts: 78
ceek79 wrote:
Code:
getSession().createQuery("from Message m order by m.date desc").setMaxResults(1);


because hibernate add's a restriction to the query like "LIMIT 1" ... the other solution prepares a result set with all message's

That is what I thought. The other one creates a list first and picks the first item.


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.